Get all maps that contain a feature layer - Python API

7277
10
Jump to solution
10-07-2020 06:38 AM
TanGnar
Occasional Contributor

Is it possible to find all of the maps that contain any one feature layer in ArcGIS Online using the Python API? For example, if there is a feature layer using frequently across the organization, and I want to find out which maps are using that layer.

I see some documentation about finding relationships between items here. The Map2Service seems to work in one direction - find the services in the map. But is there a way to do this the other direction? 

10 Replies
LukeSavage
Occasional Contributor II

""" Performed some updates for newer versions of Python API in an ArcGIS Pro 3.1.x setting which got rid of the PropertyMap Attribute error.
Date: 10-23-2023
"""

def find_maps_with_layer(lyr_to_find, map_item_query='1=1'):
web_map_items = gis.content.search(query=map_item_query, item_type="Web Map", max_items=10000)
print(f"Searching {len(web_map_items)} web maps")
maps_with_layer = []

for item in web_map_items:
found_it = False
wm = WebMap(item)
lyrs = wm.layers

for lyr in lyrs:
if hasattr(lyr, 'url') and lyr.url is not None:
if lyr_to_find in lyr.url:
found_it = True
break

if found_it:
maps_with_layer.append(item)

print(f"Found {len(maps_with_layer)} maps which contain the layer")
return maps_with_layer

# Parameters:
lyr_to_find = 'https://<domainurl>/<webadaptor>/rest/services/<nameoffeatureservice>/FeatureServer'
map_item_query = "Title:*"

# Get a list of web maps with the layer:
maps_with_layer = find_maps_with_layer(lyr_to_find, map_item_query)

# Print the titles of web maps containing the layer:
for item in maps_with_layer:
print(f'Web Map Title: {item.title}')

 

LukeSavage_0-1697218810966.png

 

0 Kudos