Created
January 4, 2024 14:22
-
-
Save jdcarls2/717f79ff12a4a1cb337dafe8e76c70df to your computer and use it in GitHub Desktop.
Search for AGOL Dependencies given an ItemID or layer URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This will look for any items that reference a layer or service. | |
# Given an ItemID, it will find the service URL (or you can specify the URL directly) | |
# The script will look through every map and app in your org to see if the URL is referenced | |
# Matching items will be printed out at the end | |
from arcgis.gis import GIS | |
import pandas as pd | |
gis = GIS("home") # or your portal URL; assuming this is run in an AGOL Notebook | |
# Enter the ItemID of the thing you're interested in. | |
# Leave the first input blank to search by the service URL instead. Useful if you're looking for a specific sub-layer of the service. | |
find_id = input('ItemID: ') | |
find_url = gis.content.get(find_id).url if find_id else input('Service URL: ') | |
print(find_url) | |
# Find Maps | |
webmaps = gis.content.search('', item_type='Web Map', max_items=-1, outside_org=False) | |
map_list = [m for m in webmaps if str(m.get_data()).find(find_url) > -1] | |
# Find Apps | |
apptypes = ['Application', 'Dashboard', 'Story Map', 'Web Experience'] | |
webapps = [item for sublist in [gis.content.search('', item_type=t, max_items=-1, outside_org=False) for t in apptypes] for item in sublist] | |
app_list = [] | |
for w in webapps: | |
try: | |
wdata = str(w.get_data()) | |
criteria = [ | |
wdata.find(find_url) > -1, | |
wdata.find(find_id) > -1, | |
any([wdata.find(m.id) > -1 for m in map_list]) | |
] | |
if any(criteria): | |
app_list.append(w) | |
# Some apps don't have data, so we'll just skip them if they throw a TypeError | |
except: | |
continue | |
# Merge lists into single dataframe | |
dependencies = pd.concat( | |
[ | |
pd.DataFrame([{'title':a.title, 'id':a.id, 'type':a.type, 'url':f'{gis.url}/home/item.html?id={a.id}', 'data':a.get_data()} for a in app_list]), | |
pd.DataFrame([{'title':m.title, 'id':m.id, 'type':m.type, 'url':f'{gis.url}/home/item.html?id={m.id}', 'data':m.get_data()} for m in map_list]) | |
] | |
) | |
dependencies.reset_index(inplace=True, drop=True) | |
# Output results; if you're running this in an interactive setting, you could just view the dataframe or look at individual rows | |
# There are commas in the output, so we use a custom separator here | |
dependencies.to_csv('dependencies.csv', sep='|', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment