# Replacing data source of ArcMap Service Layer using ArcPy?

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"]
destURL = "https:\path\to\image\service"

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

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)