Change MXD data sources to another SDE geodatabase

3853
6
07-18-2011 11:14 AM
JakeSkinner
Esri Esteemed Contributor
Is it possible to change the data source of an MXD to another SDE geodatabase?  I've tried this, but have only been able to get this to work when switching versions within the same SDE geodatabase.  Ex:

import arcpy
from arcpy import env
env.workspace = workspace = r"C:\temp\python"

env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd2 = workspace + "\\" + mxd
    mapdoc = arcpy.mapping.MapDocument(mxd2)
    mapdoc.findAndReplaceWorkspacePaths(r"C:\TEMP\Python\VECTOR_Default.sde", r"C:\TEMP\Python\VECTOR_QAQC.sde")
    mapdoc.save()

del mapdoc


The above code works as I am switching from one version (SDE.Default) to another version (SDE.QAQC) within the same VECTOR geodatabase.  When I try to switch to another SDE geodatabase that contains the same feature class (same owner and feature class name, just different database), it does not work.  Ex:

import arcpy
from arcpy import env
env.workspace = workspace = r"C:\temp\python"

env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd2 = workspace + "\\" + mxd
    mapdoc = arcpy.mapping.MapDocument(mxd2)
    mapdoc.findAndReplaceWorkspacePaths(r"C:\TEMP\Python\VECTOR_Default.sde", r"C:\TEMP\Python\RASTER_Default.sde")
    mapdoc.save()

del mapdoc


Thanks for any suggestions!
Tags (2)
6 Replies
RuthEmerick
New Contributor II
It sure is. I used replaceDataSource instead of findAndReplaceWorkspacePaths because it works better with group layers.

Attached is a copy of the script I used for all our mxds when we switched to ArcServer 10. It is meant to be run from the python interpreter in an open mxd, and does lots of extra things like copying files from one place to another that we needed so we could deploy this for non-techincal users. You can adapt this for a simpler case (we had a lot of name changes, too) and for looping through mxds in a specified folder; there is no need for the mxd to be open if the parameters are set correctly.

Have fun!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Thanks, Ruth!  The 'replaceDataSource' method was exactly what I needed.  I was able to get this working with the following code:

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = workspace = arcpy.GetParameterAsText(0)

env.overwriteOutput = True

mxdList = arcpy.ListFiles("*.mxd")

for mxd in mxdList:
    mxd = workspace + "\\" + mxd
    print mxd + " is being processed"
    mxd = mapping.MapDocument(mxd)
    for df in mapping.ListDataFrames(mxd, "*"):
        for lyr in mapping.ListLayers(mxd, "*", df):
            lyr.replaceDataSource(arcpy.GetParameterAsText(1), "SDE_WORKSPACE", "")
            print "Successfully updated data sources"
    mxd.save()

del mxd
Lake_Worth_BeachAdmin
Occasional Contributor III

How do i find the path to the different versions in my SDE ? to use this method ( switch data sources from one version to another)

I only see the SDE name not the path for different versions? 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Joe Head‌ the version will be controlled through the .sde connection file.  If you wan to connect to a different version, create a connection to your enterprise geodatabase > right-click on the connection > Geodatabase Connection Properties.  Below is a helpful document:

Connect to a specific geodatabase version—Help | ArcGIS Desktop 

0 Kudos
Lake_Worth_BeachAdmin
Occasional Contributor III

I understand how to do this with the GUI but I wanted to write a script to change all Layer Data sources within an MXD from Version 1 to version 2 within the same SDE. 

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Joe Head‌ once you create the .sde connection file to version 2 using the GUI, have the script point to this .sde connection file and you're all set.  You can also create the .sde connection file using the Create Database Connection tool.