PROBLEM

Attempts to find and replace multiple data sources from enterprise geodatabase connections using Python fail

Last Published: July 21, 2020

Description

Attempts to use the findAndReplaceWorkspacePaths() function to update layers between two differently named enterprise geodatabases fail, and the layers are not updated.

Cause

The findAndReplaceWorkspacePaths() function works with a file geodatabase, but is not intended for use to switch between two differently named enterprise geodatabases. This is a known limitation. The script fails because the schema for the layer includes the name of the database, hence failing to connect the layers correctly.

Solution or Workaround

As a workaround, use the lyr.replaceDataSource() function.

  1. Import the ArcPy module.
import arcpy
  1. Specify the desired map paths and enterprise geodatabase connection paths.
Map = r'D:\Sample\Sample1.mxd'
NewMap = r'D:\Sample\Sample2.mxd'
FindDB = r'Database Connections\OriginalDB@sa.sde'
ReplaceDB = r'Database Connections\abc.sa.sde'

mxd = arcpy.mapping.MapDocument(Map)
layerList = arcpy.mapping.ListLayers(mxd)
  1. Create a loop to find all the layers and update the path.
for lyr in arcpy.mapping.ListLayers(mxd):
    ds = lyr.dataSource
    if lyr.supports("DATASOURCE"):
        if FindDB in ds:
            print "Layer: " + lyr.name + " Source: " + lyr.dataSource
            lyr.replaceDataSource(ReplaceDB, "SDE_WORKSPACE", "", False)
  1. Save the new map.
mxd.saveACopy(NewMap)
print lyr.dataSource

The following shows the full script:

import arcpy

Map = r'D:\SharedDrive\PathSwitch.mxd'
NewMap = r'D:\SharedDrive\PathSwitch4.mxd'
FindDB = r'Database Connections\OriginalDB@sa.sde'
ReplaceDB = r'Database Connections\abc.sa.sde'

mxd = arcpy.mapping.MapDocument(Map)
layerList = arcpy.mapping.ListLayers(mxd)
for lyr in arcpy.mapping.ListLayers(mxd):
    ds = lyr.dataSource
    if lyr.supports("DATASOURCE"):
        if FindDB in ds:
            print "Layer: " + lyr.name + " Source: " + lyr.dataSource
            lyr.replaceDataSource(ReplaceDB, "SDE_WORKSPACE", "", False)
mxd.saveACopy(NewMap)
print lyr.dataSource

Article ID: 000022907

Software:
  • ArcMap
  • ArcGIS Server

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options