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
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN>os <SPAN class="keyword token">from</SPAN> arcpy <SPAN class="keyword token">import</SPAN> env env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"WORKSPACEPATH"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> lyr<SPAN class="punctuation token">.</SPAN>workspacePath lyr<SPAN class="punctuation token">.</SPAN>findAndReplaceWorkspacePath<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>workspacePath<SPAN class="punctuation token">,</SPAN><SPAN class="string token">"Database Connections\\gis.sde"</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">"TRUE"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> lyr<SPAN class="punctuation token">.</SPAN>dataSource<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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-d682e01df9b7&searchIndex=0 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&searchId=0c2f92b4-77f3-459c-89c4-c71ea7e1e142&searchIndex=1 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.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.