PROBLEM
Attempts to use the findAndReplaceWorkspacePaths() function to update layers between two differently named enterprise geodatabases fail, and the layers are not updated.
The findAndReplaceWorkspacePaths() function works with a file geodatabase, but is not intended for use to switch between two differently named enterprise geodatabases. This is a known limitation. The script fails because the schema for the layer includes the name of the database, hence failing to connect the layers correctly.
As a workaround, use the lyr.replaceDataSource() function.
import arcpy
Map = r'D:\Sample\Sample1.mxd' NewMap = r'D:\Sample\Sample2.mxd' FindDB = r'Database Connections\OriginalDB@sa.sde' ReplaceDB = r'Database Connections\abc.sa.sde' mxd = arcpy.mapping.MapDocument(Map) layerList = arcpy.mapping.ListLayers(mxd)
for lyr in arcpy.mapping.ListLayers(mxd): ds = lyr.dataSource if lyr.supports("DATASOURCE"): if FindDB in ds: print "Layer: " + lyr.name + " Source: " + lyr.dataSource lyr.replaceDataSource(ReplaceDB, "SDE_WORKSPACE", "", False)
mxd.saveACopy(NewMap) print lyr.dataSource
The following shows the full script:
import arcpy Map = r'D:\SharedDrive\PathSwitch.mxd' NewMap = r'D:\SharedDrive\PathSwitch4.mxd' FindDB = r'Database Connections\OriginalDB@sa.sde' ReplaceDB = r'Database Connections\abc.sa.sde' mxd = arcpy.mapping.MapDocument(Map) layerList = arcpy.mapping.ListLayers(mxd) for lyr in arcpy.mapping.ListLayers(mxd): ds = lyr.dataSource if lyr.supports("DATASOURCE"): if FindDB in ds: print "Layer: " + lyr.name + " Source: " + lyr.dataSource lyr.replaceDataSource(ReplaceDB, "SDE_WORKSPACE", "", False) mxd.saveACopy(NewMap) print lyr.dataSource
Article ID: 000022907
Get help from ArcGIS experts
Download the Esri Support App