lstMXDs = glob.glob(env.workspace + "\\" + "*.mxd") for mxd in lstMXDs: mxd = mapping.MapDocument(mxd) for df in mapping.ListDataFrames(mxd, ""): lstLayers = mapping.ListLayers(mxd, "*", df) for lyr in lstLayers: if "vector" in lyr.dataSource: print lyr.dataSource lyr.replaceDataSource("arcsde_DIRECT.sde", "SDE_WORKSPACE", "") print "Successfuly updated data sources" else: print lyr.dataSource lyr.replaceDataSource("RASTER.sde", "SDE_WORKSPACE", "") print "Successfully updated data sources" mxd.save() del mxd
Hello Im trying to solve a similar problem ..
We have moved from multiple sde databases to one single database
I'm trying replace database connections in an existing mxd with connection name rather than having paths based on users. and then replace the database connection to the new sde database connection.
I'm looking to accomplish this in a two step approach by using find and replace workspace paths first and replace Datasource later.
I have figured the later part but this is where I'm stuck and manually replacing the paths for each mxd using the set datasource option on arccatalog.
C:\Users\jdoe\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\gis.sde
C:\Users\djohn\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\gis.sde
import arcpy,os
from arcpy import env
env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("WORKSPACEPATH"):
print lyr.workspacePath
lyr.findAndReplaceWorkspacePath(lyr.workspacePath,"Database Connections\\gis.sde","TRUE")
print lyr.dataSource
This is the error that i get
File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\_mapping.py", line 696, in findAndReplaceWorkspacePath
return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error
Any direction on how to resolve this will be appreciated.
Thanks
It would help if you reposted you code /blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=147137e7-ef65-4684-ba98-d682e01... so any indentation errors can be eliminated.
May be overkill, but I have a /blogs/myAlaskaGIS/2015/08/31/python-addin-for-data-inventory-and-broken-link-repair?sr=search&searc... which might help. If the links aren't currently broken, just rename the .sde connection file to make them broken and see if it lists them. Test on a copy of mxd's in a test folder first.
Hello Rebecca,
Thanks for the heads up to post code with formatting.
I have figured it out each user has named the connection to the database differently and added if else statements to eliminate the unexpected error msg.
Thanks