3

I have many map documents that point to a hosted imagery service layer that has changed names.

I would like to write an ArcPy script that will replace the data sources of service layers that have changed names. So far I have tried the following from the Python window, without success (assuming the only layer in the MXD is the service layer):

mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd)[0]
sourceURL = lyr.serviceProperties["URL"]
arcpy.AddMessage("Source URL: {0}".format(sourceURL))
destURL = "https:\path\to\image\service"
arcpy.AddMessage("Destination URL: {0}".format(destURL))

lyr.findAndReplaceWorkspacePath(sourceURL, destURL)

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.save()
  • What happens when you try that? Do you get any errors, or nothing changes at all? Or something else? – Midavalo Jun 30 '16 at 18:29
  • I assume connectionProperties is a dictionary with your new values? – Midavalo Jun 30 '16 at 18:31
  • @Midavalo, I do not get any errors, but when I re-inspect the layer service properties, the URL has not changed. Same thing if I save the MXD, close, reopen, and re-inspect. It appears nothing has changed. I've updated the destURL variable to be more generic. – RyanDalton Jun 30 '16 at 19:35
4

I don't believe it is possible to do it this way, and the reason is because service layers do not support workspace paths. You can test this on your layer by lyr.supports("WORKSPACEPATH"), which should return False. However you could use UpdateLayer to replace the old service layer with a new one you create with the correct URL. I'm pretty sure to do it this way, though, the mxd can't be opened, so you'd have to run it from a standalone script:

mxd = arcpy.mapping.MapDocument(path to your .mxd)
df = arcpy.mapping.ListDataFrames(mxd)[0]
newLayer = arcpy.mapping.Layer(new service .lyr)
oldLayer = arcpy.mapping.ListLayers(mxd)[0]
arcpy.mapping.UpdateLayer(df, oldLayer, newLayer, False)
mxd.save()

http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/updatelayer.htm

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.