Using replaceDataSource with an SDE Feature Dataset\FeatureClass

4428
3
04-28-2015 01:10 PM
JonBarlett
New Contributor

I am trying to change the data source from an FGDB to an SDE feature class.  I can get this to work for an SDE feature class that is not within a feature dataset.  For the life of me, I cannot get this to work for a feature class within a feature dataset.  The script fails on line 15.

The error message:

ValueError: Layer: Unexpected error

I believe I am not correctly  specifying the workspath_path or dataset_name correctly.  The destination dataSource =

Database Connections\sdetest.sde\PIPELINE.BAKKEN\PIPELINE.Pipe    

SDE Connection: Database Connections\sdetest.sde

Dataset: PIPELINE.BAKKEN

Feature Class: PIPELINE.Pipe

Any help would be appreciated!

import arcpy
import os
mxd = arcpy.mapping.MapDocument('CURRENT')
ListLayers = arcpy.mapping.ListLayers(mxd)
workspace_path = "Database Connections\sdetest.sde"
dataset_name = "PIPELINE.BAKKEN\PIPELINE.Pipe"
# dataset_name = "BAKKEN\PIPELINE.Pipe"
# dataset_name = "PIPELINE.BAKKEN.PIPELINE.Pipe"
# Source: Database Connections\sdetest.sde\PIPELINE.BAKKEN\PIPELINE.Pipe
for Layer in ListLayers:
    if Layer.supports("DATASOURCE"):
        desc = arcpy.Describe(Layer)

        if desc.catalogPath == "\\\\ftwnas01a\\ProjectsNew\\NatGasOps\\NGOData\\Assets\\Bakken.gdb\\Bakken\\Pipe":
           Layer.replaceDataSource(workspace_path, "SDE_WORKSPACE", dataset_name)
0 Kudos
3 Replies
BlakeTerhune
MVP Regular Contributor

Have you tried using the find and replace data Source feature in ArcCatalog?

ArcGIS Help 10.1 - Setting data sources

RebeccaStrauch__GISP
MVP Emeritus

If in 10.3 Updating and fixing data sources—ArcPy | ArcGIS for Professionals

I'm working on updating data sources with arcpy myself right, not just FGDB/SDE, so I'm looking at all of it too.  Looks like they recommend updating/replacing the database connection file.  #4 on that help page

import arcpy
aprx = arcpy.mp.ArcGISProject(r'C:\Projects\YosemiteNP\Yosemite.aprx')
aprx.updateConnectionProperties(r'C:\Projects\YosemiteNP\Vector_Data\Yosemite.gdb',
                                r'C:\Projects\YosemiteNP\DBConnections\Server.sde')
aprx.save()
del aprx

I'm not sure if that will work for you or not, but might be worth checking.

JonBarlett
New Contributor

Thanks Blake.  I am really trying to automate this through scripting.  We are converting over 100 FGDB feature classes to SDE, which will affect well over 100 ArcMap projects.  If I can't figure this out, then yes, that will probably be how we update our mxds.

0 Kudos