Change db connection properties of layer files in a folder (SDE to SDE)

573
8
11-07-2021 11:43 AM
JavadSalimi
New Contributor III

I would really appreciate some of you ESRI and Python experts to lend a hand to me on this subject. I have a number of folders that each contain countless layer files (.lyr). All of the layer files reference an old SDE db connection and the task at hand is to change them all to a new SDE connection using Python. 

There seems to be a lot of forum submissions describing a similar requirement but not ending well due to some limitations with objects or python, or some combination thereof. I am hoping to get any insight at this point. A lot of examples involve changing the data connection prooperties of layers in a map (.mxd), but in my case, they are not managed this way...they're in file folders. 

Something along the lines of:

for every layer file in a folder change the db connection from SDE1 to SDE2

0 Kudos
8 Replies
DanPatterson
MVP Esteemed Contributor

Updating and fixing data sources—ArcGIS Pro | Documentation would this be what you are referring to?


... sort of retired...
0 Kudos
JavadSalimi
New Contributor III

Thanks for taking the time to reply Dan.

I actually experimented with the suggestions on the link you provided. However I am hoping to stick with .lyr files. The apps  dependant on the layer files may not be able to handle .lyrx files.

0 Kudos
TigerWoulds
Occasional Contributor III

You can try something like within your for loop

lyr.findAndReplaceWorkspacePath(find_workspace_path=lyr.dataSource, replace_workspace_path=r"C:\Newpath\To\SDE_ConnectionFile.sde")

Lots of examples here: Desktop Help 10.0 - Updating and fixing data sources with arcpy.mapping (arcgis.com)

0 Kudos
JavadSalimi
New Contributor III

Thank you for the response. 

There is some promise here. I have the following so far but after the "for", only the print line works, the others return the error "AttributeError: 'unicode' object has no attribute 'findAndReplaceWorkspacePath'". 

import arcpy
import os

arcpy.env.workspace = r"\\...\layerfiletest"
lyrFileList = arcpy.ListFiles("*.lyr")
for lyr in lyrFileList:
            #print lyrFile
            #lyr.findAndReplaceWorkspacePaths(r"\\...User.sde", r"\\...User.sde")
            #lyr.findAndReplaceWorkspacePath(find_workspace_path=lyr.dataSource, replace_workspace_path=r"\\...User.sde")
            #lyr.findAndReplaceWorkspacePath(lyr.dataSource, r"\\...User.sde")

0 Kudos
DanPatterson
MVP Esteemed Contributor

python isn't going to understand your path  r"\\...User.sde"

the error is in the path name, so you will have to provide it or check for spaces, international characters, punctuation and the like

 


... sort of retired...
0 Kudos
JavadSalimi
New Contributor III

Sorry to confuse the matter. I have a proper path but just tried hiding it. Please assume they are correct.

0 Kudos
TigerWoulds
Occasional Contributor III

If you can post the full code you are using (change folder and drive letter names if you want) that is a better option. You can also try lyr.replaceDataSource

0 Kudos
JavadSalimi
New Contributor III

I don't have much more but, for clarity I have posted with the full paths:

import arcpy
import os

folderpathLayerFiles = r"\\corp\roaming\redirected\jds\Desktop\test\layerfiletest2"
lyrFileList = arcpy.ListFiles("*.lyr")
for lyr in lyrFileList:
           #print lyrFile
           #lyr = arcpy.mapping.Layer(os.path.join(folderpathLayerFiles,lyrFile))
           #print lyr.dataSource
           #lyr.findAndReplaceWorkspacePath(find_workspace_path=lyr.dataSource,    replace_workspace_path=r"\\server\gis_work\GIS_Layers\Connection File\User@SDE.sde")
          #lyr.findAndReplaceWorkspacePath(lyr.dataSource, r"\\server\gis_w\GIS_Layers\Connection File\User@SDE.sde")
          #lyr.findAndReplaceWorkspacePath(r"\\server\gis_w\GIS_Layers\User@SDE.sde", r"\\server\gis_work\GIS_Layers\Connection File\User@SDE.sde")

I tried lyr.replaceDataSource as well but go the same error.

 

 

0 Kudos