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

07-19-2016 08:22 AM
New Contributor

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





0 Kudos
2 Replies
MVP Regular Contributor

Have you looked through the examples in the documentation?

Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop

You could also consider findAndReplaceWorkspacePath(). In your code, what is wrkspcInfra and newStation?

Also, there is a syntax highlighting feature for posting code blocks.

New Contributor

Thank you for your time Blake!

As for the first comment, I have looked at it and that is how I got an idea of using replaceDataSource method to change the data source.

For the second, the path() method seems not allowing to specify a new name even though it can change the path of workspace.

Regarding the third, those are two variables (wrkspcInfra, newStation) for SDE workspace (i.e., feature dataset) and feature classes respectively. To illustrate, I set the variables as follow:


#Set up a variable for
new workspace; newly organized under sde feature dataset

#wrkspcInfra = r'Database Connections\Connection to sde(sdeprod - user).sde\{}'.format(listDS[16]) #Infrastructure

wrkspcInfra = r'Database Connections\Connection to sde(sdeprod - user).sde\GISADMIN.INFRASTRUCTURE'

#Set up a variable for sde new dataset (feature class under the feature dataset above)


newStation = r'Database Connections\Connection to sde(sdeprod - user).sde\GISADMIN.INFRASTRUCTURE\GISADMIN.INF_LIRR_GUI_STA_STATIONS_PT'


As a matter of fact, I am trying different variations to set up parameters for the replaceDataSource methods as shown above. However, I haven't have that much of luck to make it running. As a reference, please see an example of feature dataset and feature class under SDE database below:



Lastly, the syntax highlighter looks very useful. I will look into this for future usage.

0 Kudos