HOW TO
It is not possible to directly replace layers that source data from a local disk with a feature service layer. However, it is possible to recreate the layer as a feature service layer into a map document and insert the new layer into the map document using the arcpy.mapping method, InsertLayer(). The old layer can be removed by using the RemoveLayer() method.
Import arcpy
#Set the workspace to the desired output layer files.
arcpy.env.workspace = r"D:\Cases\01800498_SourceFeatureService"
#Specify the desired map document.
mxd = arcpy.mapping.MapDocument("CURRENT")
#Create a Python list of layers in the mxd
layers = arcpy.mapping.ListLayers(mxd)
#Iterate through the layer list and create a layer file for each layer in the list.
for layer in layers:
arcpy.SaveToLayerFile_management(layer, layer.name)
serviceList = []
for layer in layers:
serviceList.append(layer.name)
for layer in layers:
if layer.name in serviceList:
serviceLayer = arcpy.mapping.Layer(ws + "\\" + layer.name + ".lyr")
arcpy.mapping.InsertLayer(df, layer, serviceLayer, "AFTER")
arcpy.mapping.RemoveLayer(df, layer)
Article ID: 000013565
Get help from ArcGIS experts
Start chatting now