I'm trying to replace the datasource for all of the layers in my map document using Python but I keep running into an error. Right now, all of the layers point to a feature class called GISDB.DBO.TESTFEATURECLASS and I want them to point to a view that I built using the sdetable -o createview command. It has both the object id and geometry columns from the original feature class. It shows up in ArcCatalog with the same icon as a feature class and I can add it as a layer in ArcMap.

Runtime error <type 'exceptions.ValueError'>: Layer: Unexpected error

mapdoc = arcpy.mapping.MapDocument("CURRENT")
workspace_path = r'Database Connections\TestGIS.sde'
workspace_type = "SDE_WORKSPACE"
for layer in arcpy.mapping.ListLayers(mapdoc):
    layer.replaceDataSource(workspace_path, workspace_type, dataset_name)

How can I determine which property is causing the error?

  • I had some troubles with .replaceDataSource when doing layer repointing in an mxd with arcpy. I think some of the problem was updating the mxd and saving it. You may be repathing it but then you don't save it. I'll try to find some code that ended up working for me. – Justin Jul 20 '12 at 3:28
  • 1
    arcpy.mapping.UpdateLayer(p1,p2,p3,false) ...may also help. p1 is parameter 1. – Justin Jul 20 '12 at 4:04
  • I asked a similar question a while ago gis.stackexchange.com/questions/29185/…, I was getting the same error as you. Turns out I had to add mxd.save() at the end to ensure the data source was replaced. – Cindy Jayakumar Jul 20 '12 at 11:46
  • @Arabella I ran into the save error when I replaced the layer workspaces from a Python window in ArcCatalog. I think the problem there was that I hadn't allowed the mxd file to be closed because closing all instances of ArcCatalog and ArcMap and starting fresh allowed me to make my changes and save them. – jrockers Jul 23 '12 at 15:05
  • @Justin updateLayer would work but I don't want to replace the symbology. I think the underlying problem is that the string I'm using for either the workspace or the datasetName is wrong but I don't know where to find the correct string. Thanks for your comments. – jrockers Jul 23 '12 at 15:09

I am assuming that you are running this from the Python window of ArcMap.

Consequently, I would expect a little more to the error message.

Perhaps try including the following as the fifth line in your code snippet to see if that sheds more light:

print arcpy.mapping.ListLayers(mapdoc)

It may help to determine whether it is your first or a particular layer that is throwing the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.