I am trying to repoint layer (.lyr) files to a new SDE connection. In a test project folder I have two test layer files.
When using lyr.findAndReplaceWorkspacePath()
the script runs with no errors, but only the first layer in the directory gets properly updated to point to the new SDE path.
When using lyr.replaceDataSource()
the script produces an error while attempting to repoint the second layer to the new SDE path:
ValueError: Layer: Unexpected error
(Error is referring to the line containing lyr.replaceDataSource()
)
So something is running afoul on the second iteration of the for loop.
I have tested multiple .lyr files, and it's nothing wrong with any of the actual .lyr files. It's just whichever .lyr file is up second. So whichever is first, is properly repointed to SDE, and whichever is second, is not.
import arcpy, os
layersFolder = r"I:\FolderX\Kristen\FolderY\Repoint_lyrs\TestLayers\MultipleLayers"
newSDEpath = r"I:\FolderX\Kristen\FolderY\Repoint_lyrs\gis_sde.sde"
for dirname, dirnames, filenames in os.walk(layersFolder):
for filename in filenames:
path = os.path.join(dirname, filename)
basename, extension = os.path.splitext(path)
if extension == ".lyr":
lyr = arcpy.mapping.Layer(path)
if lyr.isFeatureLayer:
if lyr.supports("DATASOURCE"):
lyrName = lyr.datasetName
print lyrName
print '\t' + "newSDEpath: " + newSDEpath
print '\t' + "lyr.datasetName: " + lyr.datasetName
lyr.replaceDataSource(newSDEpath, "SDE_WORKSPACE", lyr.datasetName, True)
#lyr.findAndReplaceWorkspacePath(lyr.dataSource, newSDEpath, True)
lyr.save()
print '\t' + "Changed path: " + lyr.dataSource + '\n'
del lyr