I have a few basemaps published to ArcGIS Server. I have a script that I am running which removes these basemap map services. What I need to do is find the source REST endpoints of these mapservices to further narrow down my script. I have just added as per @Vince suggestion the isServiceLayer as an if statement and tried printing out the serviceProperties but the script throws an error on certain layers

sample UPDATED code...

f = path 
mxd = arcpy.mapping.MapDocument(r'{}'.format(f))
for df in arcpy.mapping.ListDataFrames(mxd):
    for x in arcpy.mapping.ListLayers(mxd,data_frame=df):
        if x.isGroupLayer:
            name = str(x)
            if name.startswith("Basemaps"):
                if x.isServiceLayer:
                    print x.serviceProperties


_Basemaps_hcgis\ Region
{u'UserName': u'arcgis', u'Supportsrsa': u'-1', u'ServiceType': u'MapServer', u'Connectionfile': u'C:\\Users\\\\AppData\\Roaming\\ESRI\\Desktop10.3\\ArcCatalog\\arcgis on SERVER (admin).ags', u'Connectionmode': u'1', u'URL': u'http://SERVER', u'Servertype': u'1', u'Restversion': u'10.3', u'Rsaurl': u'http://SERVER', u'Server': u'', u'Adminurl': u'http://SERVER', u'Resturl': u'http://SERVER', u'Soapurl': u'http://SERVER', u'Handshakefinished': u'-1', u'Anonymous': u'0', u'Usedefaultstagingfolder': u'-1', u'Admintokenurl': u'http://SERVER', u'Stagingfolder': u'C:\\Users\\\\AppData\\Local\\Temp\\arc434C\\Staging'}
_Basemaps_hcgis\ Region\>570000
Traceback (most recent call last):
  File "C:\Users\Desktop\blah.py", line 11, in <module>
    print x.serviceProperties
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjects\_base.py", line 78, in _get
    (attr_name, self.__class__.__name__))
NameError: The attribute 'serviceProperties' is not supported on this instance of Layer.

UPDATE from @Alex suggestions:

my mapserver basemaps are in a group layer named Basemaps and I only want to remove those basemaps... the problem is I am running this script through network drives so some of the old MXDS(before my time) have two group layers called Basemaps group layer, 1 from the old server and 1 from the new one. I DO NOT want to remove the new **Basemaps ** group layer and the only way to accomplish this is to open up each mapservice and check which server it is from... here is a picture of how it is set up

enter image description here

  • 1
    Your looping is suspect -- ListLayers does not restrict by data frame (your code would not work with multiple data frames). The Layer documentation lists the properties, including .isServiceLayer and .serviceProperties
    – Vince
    Mar 16 '18 at 13:47
  • @Vince so remove for df in arcpy.mapping.ListDataFrames(mxd): and I will be able to loop through all the dateframes? because that is what I am trying to do
    – ziggy
    Mar 16 '18 at 13:49
  • i call arcpy.mapping.RemoveLayer(df,x) don't I need to reference the dataframe for that?...
    – ziggy
    Mar 16 '18 at 13:50
  • 1
    Look at your logic. You'd act on all layers as being in any data frame. You need to specify ListLayers(...,data_frame=df) to use your current looping structure
    – Vince
    Mar 16 '18 at 13:54
  • gotcha okay I see what you mean I fixed it
    – ziggy
    Mar 16 '18 at 14:03

The ArcGIS map services are added to the map document as group layers. This means you need to check whether a layer is both a group layer AND a service layer.

This code will remove all layers that are ArcGIS Server services and the server machine they come from has oldservername in the URL. There are other properties which you can use instead of the Resturl, consult the docs.

mxd = arcpy.mapping.MapDocument('current')
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):    
        if lyr.isGroupLayer and lyr.isServiceLayer:
            if 'oldservername' in lyr.serviceProperties['Resturl']:
                arcpy.mapping.RemoveLayer(df, lyr)

This code will find all ArcGIS Server based map services and remove them from the map document. I use the current map document for simplicity.

  • updated my question!
    – ziggy
    Mar 16 '18 at 15:58
  • @ziggy, this code will remove all the ArcGIS Server map services regardless of where they are in the TOC (under Basemaps or not). Is this working for you? Mar 16 '18 at 16:47
  • yes but I was able to do that before I asked this question... I really need to open up each service layer and check to see which server it is on, its on the old one then remove it. sorry if I have unclear I did not set up any of these mapservices at my work so im learning on the fly
    – ziggy
    Mar 16 '18 at 17:23
  • should I ask a new question?
    – ziggy
    Mar 16 '18 at 17:26
  • np, updated the answer Mar 16 '18 at 20:52

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.