English

How To: Update ​enterprise geodatabase data sources for map documents using ArcPy

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

Related Information

Last Published: 9/27/2021

Article ID: 000017448

Software: ArcGIS Pro 2.8.3, 2.8.2, 2.8.1, 2.8, 2.7.4, 2.7.3, 2.7.2, 2.7.1, 2.7, 2.6.7, 2.6.6, 2.6.5, 2.6.4, 2.6.3, 2.6.2, 2.6.1, 2.6