AnsweredAssumed Answered

Changing layer datasources

Question asked by ginomellino on Nov 8, 2017
Latest reply on Nov 13, 2017 by rastrauch

Hi All, 

I have an MXD which contains a large number of layers that originally all linked to featureclasses within a single file geodatabase. I have since needed to move some of the data into another file geodatabase. My thinking was that after I moved the data I could use arcpy to iterate the layers, pick up any with broken sources and change their data source to target the new geodatabase. I have the code below which I adapted from HERE to suit my needs but it throws the error below below in red. I'm sure it is something simple but can anyone tell me what I am doing wrong here? The name printed by line 7 (i.e. State Electorates) is that or a featureclass so i don't see why it wouldn't work?

 

Many thanks in advance. 

 

Gino

 

import arcpy
mxd = arcpy.mapping.MapDocument(r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\Copy_of_Internal_Geodatabase_ArcGIS10.3(1).mxd")
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        if lyr.isBroken:
            if "DYNAMIC_DATA.gdb" in lyr.dataSource:
                print lyr.name
                lyr.findAndReplaceWorkspacePath(lyr.workspacePath, r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\STAND_ALONE_DATA.gdb")
mxd.saveACopy(r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\Updated_Internal_Geodatabase_ArcGIS10.3.mxd")
del mxd

 

>>>
State Electorates
Traceback (most recent call last):
File "E:\_GINO's\Planning Geodatabase\Geodatabase Management\Script\updateDS.py", line 8, in <module>
lyr.findAndReplaceWorkspacePath(lyr.workspacePath, r"E:\_GINO's\Planning Geodatabase\Planning Geodatabases\Internal Geodatabase\STAND_ALONE_DATA.gdb")
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\utils.py", line 182, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\_mapping.py", line 696, in findAndReplaceWorkspacePath
return convertArcObjectToPythonObject(self._arc_object.findAndReplaceWorkspacePath(*gp_fixargs((find_workspace_path, replace_workspace_path, validate), True)))
ValueError: Layer: Unexpected error
>>>

Outcomes