Changing SDE Data Sources

505
3
05-16-2012 07:38 AM
JohnMax
New Contributor II
I have hundreds of mxd's with broken links due to migration to a different SDE server.  I know how to change to the new SDE connection using the findAndReplaceWorkspacePaths method.  My problem is that the Feature Dataset that the Feature Classes were previously under is renamed in the new SDE database.  This causes all of the links to still be broken.

Is there a way to fix this programmatically using Python?  I am a newbie, so any code examples would be appreciated.

Thanks!
Tags (2)
0 Kudos
3 Replies
JoshuaChisholm
Regular Contributor
Seems like you have two major tasks. 1, to access all your MXDs and 2, to switch the SDE connection.
1) I've used "os.listdir" to list out all folders/files in a directory. This assumes you are using systematic folder system. Here's a sample:
import os, arcpy

masterPath = "C:\\MXDs"
sites = os.listdir(sitesPath)

for site in sites:
if ".mxd" in site or ".MXD" in site: #checks if file is a map document
         mxd = arcpy.mapping.MapDocument(masterPath+"\\"+site)
  #==========
  #do stuff to MXDs here:

  #==========
  mxd.save()
  del mxd


2) You can either go layer by layer in the MXD or sweep for the entire MXD.
The layer by layer would look something like:

for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.isFeatureLayer and ".sde" in lyr.dataSource: #'isGroupLayer' must be tested before 'dataSource'
  lyr.replaceDataSource(newPathSDE, "SDE_WORKSPACE", lyr.datasetName)

The entire MXD sweep would look something like:
mxd.replaceWorkspaces("C:\\connections\\OldSDEconnection.sde", "SDE_WORKSPACE", "C:\\connections\\NewSDEconnection.sde", "SDE_WORKSPACE")



Good luck,
~Josh
0 Kudos
JosephJose
New Contributor III

I used the same source but got a big exception during line replaceDataSource

0 Kudos
JohnMax
New Contributor II
After alot of frustration, I finally got this to work thanks to this thread:

http://forums.arcgis.com/threads/40806-Replace-Data-Sources-from-shapefile-to-feature-dataset-featur...

Hope this helps someone else out there.
0 Kudos