Using the mxd.findAndReplaceWorkspacePaths method, if and MXD is updated and the path the the .sde connection file moves again all of the data sources will break again.

Is there a method to permanently update each SDE layer in an MXD Table of Contents to permanently store the SDE Server and Service? Ie. Just replace the SERVER and INSTANCE for each SDE layer in an MXD using python or ArcObjects in Pyhton?

  • So far as I know, Arcmap always uses the path to the connection file, e.g. "Database Connections\Connection to Foobar.sde", and there is no way to just pass or save the SERVER and SERVICE info directly. I would be delighted to be proven wrong. I can imagine a hack to store a dynamically generated sde file in the same directory as the mxd, or even perhaps inside the mxd, and then refer to that instead; don't know how to implement it though. Commented Aug 29, 2012 at 21:35
  • 1
    @matt, I don't think that's completely correct; ArcMap will use the connection properties persisted within the layer. The SDE connection file is never looked at again, its path is only metadata. The problem here seems to be specific to the arcpy data source replacement methods. You can definitely set connection properties without using a connection file with ArcObjects (e.g. using the IWorkspaceName interface). However with arcpy you are limited to the methods provided.
    – blah238
    Commented Aug 29, 2012 at 23:00
  • @matt you are correct. I'm using the Arcpy methods. I can't get the connection properties for each layer to 'save'. A dependence for obtaining connection properties from the path to the connection file remains after an mxd.save() or mxd.saveACopy()
    – MMV
    Commented Aug 30, 2012 at 14:57

3 Answers 3


I have found a solution to breaking the dependence on path to the .sde connection file using ArcPy and the following method.

This way works:

lyr.replaceDataSource(sdeConn, "SDE_WORKSPACE", lyr.datasetName, False)
table.replaceDataSource(sdeConn, "SDE_WORKSPACE", lyr.datasetName, False)

You can rename or delete the connection file after the MXD has been repaired and saved. It will continue to open without issue after the connection file is no longer there.

These methods DO NOT work:


You CANNOT rename or delete the connection file after the MXD has been repaired. The MXD will look for the .sde connection file in the same location always each time it's opened.

  • I am using the lyr.replaceDataSource, however It seems that arcmap cannot detect the new datasource since it shows a "!" before the layer, so I have to Repair datasource by hand. Here is my code:
    – Smalis
    Commented Nov 14, 2014 at 11:58
  • 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:59
  • Your comment about the methods that for feature datasets that DOESN'T WORK has saved me a lot of failed attempts A BIG THANK YOU! I concur that this hasn't worked for me either is it a bug? the lyr.workspacePath shows the feature dataset
    – woodwa
    Commented May 8, 2018 at 6:56

You need to walk nested group layers then use arcpy.CreateArcSDEConnectionFile_management to create new sde using parameters from lyr.serviceProperties, and pass parameters as servProp.get or arcpy.GetParameterAsText(0).

Don't forget to use os.remove(temp_sde) if os.path.exsists(temp_sde) else None.

for maplayer in lyr_list:
    servProp = maplayer.serviceProperties
    if maplayer.serviceProperties["ServiceType"] == "SDE":
        host = servProp.get('Server','N\A')
    if host in ("MyServer"):

This may be too simple but...
If you rename the old connection and name the new one with the old name does that accomplish a better result?

  • If you re-point every Layer in an MXD to use a connection file located at '\\corpshare\sde\corpsde.sde' everything works great. If however that path becomes unavailable, every MXD is now broken.' I am more interested in the connection properties SERVER and SERVICE within the connection file, rather than the path to it. I want to essentially update each Layers SERVER and SERVICE, so if that path becomes unavailable the MXD can still access the server without a path to a valid connection file.
    – MMV
    Commented Aug 29, 2012 at 21:07
  • @MMV, are you sure? I just tried it: 1) Add a layer to map using an SDE connection file 2) Save the MXD and close ArcMap 3) Delete/rename SDE connection file 4) Open saved MXD. The layer loads without a problem.
    – blah238
    Commented Aug 29, 2012 at 23:18
  • @Blah238 correct I have seen this - if the sde file is gone it still connects ??? What I thought is that if your change the connection properties in an sde file (to point it to another server/service that it would find it. (as ong as you don't open and break the links first. I will try it out and get back.
    – Brad Nesom
    Commented Aug 29, 2012 at 23:53
  • Just read blah238's post above. I wasn't aware of that. because I have had password issues before and fixed them by changing the connection strings. perhaps the trick is to break it and repair it with the new connection strings. but to break it you have to stop the existingg service. ??
    – Brad Nesom
    Commented Aug 30, 2012 at 0:12
  • @blah238 By manually creating an MXD in ArcMap, saving it and removing the connection file everything will continue to work after the path to the .sde file has been 'broken'. Updating an already existing MXD using ArcPy, it's where the dependence on the path to the connection file remains.
    – MMV
    Commented Aug 30, 2012 at 14:59

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.