HOW TO

Update ​enterprise geodatabase data sources for map documents using ArcPy

Last Published: September 28, 2021

Summary

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.

Procedure

The following steps describe how to update an enterprise geodatabase data source using ArcPy:

  1. Import the necessary module and specify the map document location.
import arcpy
mxd = arcpy.mapping.MapDocument(r"mxd_source_location")
  1. Specify the new enterprise geodatabase data source path.
new_sdeworkspace_path = r"new_enterprise_source_location"
  1. Loop through the layers available in the map document and replace the old source to the new source specified in step 2.
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name
    lyr.replaceDataSource(new_sdeworkspace_path, "SDE_WORKSPACE")
  1. Save the map document.
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

Article ID:000017448

Software:
  • ArcMap 10 6
  • ArcGIS Pro 2 8 x
  • ArcGIS Pro 2 7 x
  • ArcMap 10 7
  • ArcGIS Pro 2 x
  • ArcMap 10 8

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic