AnsweredAssumed Answered

lyr.replaceDataSource Question

Question asked by cwholmes on Jun 16, 2017
Latest reply on Jun 21, 2017 by cwholmes

Hello, I have the following script:

import arcpy
fileno = arcpy.GetParameterAsText(0)
#Define the path of the mxd
mxd_file = 'o:/plan/subdiv/' + fileno + '/' + fileno + '-ortho_photo.mxd'
arcpy.AddMessage('fileno: ' + fileno + ' mxd_file: ' + mxd_file)
mxd = arcpy.mapping.MapDocument(mxd_file)
for lyr in arcpy.mapping.ListLayers(mxd):
    if == 'roads':
    elif == 'railways':
    elif == 'rivers':
    elif == 'lakes':
    elif == 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon':
        lyr.replaceDataSource('O:/plan/subdiv/' + fileno + '/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')
#Remove layer from ortho_photo.mxd
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
        if == 'site_polygon':
            arcpy.mapping.RemoveLayer(df, lyr)
del mxd
Result = True

At line 17 I get the following error when executing:



Traceback (most recent call last):

File "X:\GIS\LUAM\DEV\Scripts\OtherTypes\", line 17, in <module>

lyr.replaceDataSource('O:/plan/subdiv/LOC2018-0001/data_for_item.gdb','FILEGDB_WORKSPACE','proposed_OUTLINE_PLAN_polygon - OP item Site Polygon')

File "c:\program files (x86)\arcgis\desktop10.2\ArcPy\arcpy\", line 181, in fn_

return fn(*args, **kw)

File "c:\program files (x86)\arcgis\desktop10.2\ArcPy\arcpy\", line 680, in replaceDataSource

return convertArcObjectToPythonObject(self._arc_object.replaceDataSource(*gp_fixargs((workspace_path, workspace_type, dataset_name, validate), True)))

ValueError: Layer: Unexpected error


The layer 'proposed_OUTLINE_PLAN_polygon - OP item Site Polygon' is part of a feature dataset called 'data', so I don't know if that may have anything to do with it? Although, I did read that if you are using this command with a feature dataset to just reference the geodatabase and it should work fine.


Right now I'm at a bit of a loss regarding this "Unexpected error". Any ideas appreciated. Thanks!