HOW TO
There are numerous reasons why data sources need to be repaired or redirected to different locations, for example, migrating to a new machine. Manually updating every affected map document can be overwhelming. With the arcpy.mapping scripting environment, it is possible to automate data source updates for individual layers or all layers simultaneously in a common workspace without even having to open the map document.
The following steps describe how to update an enterprise geodatabase data source using ArcPy:
import arcpy mxd = arcpy.mapping.MapDocument(r"mxd_source_location")
new_sdeworkspace_path = r"new_enterprise_source_location"
for lyr in arcpy.mapping.ListLayers(mxd): print lyr.name lyr.replaceDataSource(new_sdeworkspace_path, "SDE_WORKSPACE")
mxd.saveACopy(r"save_location_path") del mxd
The following shows a full script sample:
import arcpy mxd = arcpy.mapping.MapDocument(r"E:\Data\Mxd\test\old.mxd") new_sdeworkspace_path = r"E:\Data\Connections\GISADMIN@GEODATA.sde" for lyr in arcpy.mapping.ListLayers(mxd): print lyr.name lyr.replaceDataSource(new_sdeworkspace_path, "SDE_WORKSPACE") mxd.saveACopy(r"E:\Data\Mxd\test\new.mxd") del mxd
Get help from ArcGIS experts
Download the Esri Support App