AnsweredAssumed Answered

Replace data source of SDE layers in map documents (mxd)  with Python’s replaceDataSource method

Question asked by kkumbadaesri on Jul 19, 2016
Latest reply on Jul 20, 2016 by kkumbadaesri

Hello all,


I need some help to replace the data source of old SDE layers
in the map documents. The code outlined below is not working properly resulting
result in a broken link.


We recently reorganized our ArcSDE database resulting in all
previous feature classes reorganized under the feature dataset with a new name.
In the interim, the SDE has both old and new data.  That results in a lot of map documents, in
which the data sources of all the layers need to be replaced with new workspace
path. Eventually, we want to delete the old sde feature classes directly under
the ArcSDE.


Based on our research, lyr.replaceDataSource seems a
plausible solution since it provides an option of replacing both work space
path and dataset name.


I greatly appreciate any input.


Thank you,




#loop through the folder to locate mxds and layers within it and replace data source
of SDE layer

for fileName in os.listdir(path):

     fullPath = os.path.join(path,

if os.path.isfile(fullPath):

         basename, extension =

if extension == ".mxd":

             mxd =

print "MXD: " + fileName


             #get the list of all the layers

brknList = arcpy.mapping.ListLayers(mxd)

# Remove temporary connection file if it already exists

sdeFile = r"C:\Project\Output\TempConnection.sde"

if os.path.exists(sdeFile):

print sdeFile


#replace data source of individual SDE layer

for brknItem in brknList:

if brknItem.datasetName == 'GISADMIN.ABC_Stations':

"SDE_WORKSPACE",newStation, False)

              #elif brknItem.datasetName == 'GISADMIN.ABC_Layer':


              #     brknItem.replaceDataSource(wrkspcInfra2, "SDE_WORKSPACE",newABCLayer, False)





outmxd = basename + "_3"+ extension