I manage to change my datasouce by right clicking on an mxd file and choosing a different sde database connection file as shown in the following image. *enter image description here*

However When I try to do this using arcpy script the datasource it is not changed, but the new mxd is created (having the old datasource)

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Users\SMALIS\Documents\ArcGIS\pythonSwitchSource.mxd")
mxd.findAndReplaceWorkspacePaths(r"Database Connections\sdeConnection1.sde",r"Database Connections\sdeConnection2.sde")
del mxd

Can somebody explain me why is that happening?

I use ArcGIS 10.2 for Desktop. Oracle 11g


1 Answer 1


I have also had issues with this...This post was helpful. In our case, we have 50+ mxd's we use for web applications, each with a corresponding SDE database. We just upgraded our SDE and ArcGIS Server so I used the lyr.replaceDataSource to explicitly set the SDE workspace and it seemed to work much better. It still missed a few, but for some reason I can never get the findAndReplaceWorkspacePaths to work properly

  • import arcpy; mxd = arcpy.mapping.MapDocument(r"C:\Users\SMALIS\Documents\ArcGIS\pythonSwitchSource.mxd"); for lyr in arcpy.mapping.ListLayers(mxd): if lyr.supports("DATASOURCE"): lyr.replaceDataSource("C:\Users\SMALIS\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\DB1.sde", "SDE_WORKSPACE", lyr.datasetName, False) mxd.saveACopy(r"C:\Users\SMALIS\Documents\ArcGIS\1.mxd"); del mxd;
    – Smalis
    Commented Nov 14, 2014 at 11:41
  • 1
    I Tried your suggestion. I seems to replace the datasource and created the new mxd (1.mxd) with the new datasource. However , when I try to show the map, with data coming from the new datasource, ArcMap says that I have to repair the datasource ("!" symbol on the left of the layer in table of content area)
    – Smalis
    Commented Nov 14, 2014 at 11:42
  • Hmm, maybe try using the path r"Database Connections\DB1.sde". This is an alias for the exact same path you are using, but maybe some stuff under the hood will validate it properly.
    – crmackey
    Commented Nov 14, 2014 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.