Hello All,
I want to thank everyone for the code on this forum as this is how I started my journey. I have taken the original code and made some changes that fix some of the issues people were having. The original script was working but it was not returning some of the web apps in the list. Below is a list of changes to the code. Double check indents if not working.
- Ensures get_data() is not None.
- Increases max_items to 1000.
- Normalizes find_url for better matching (removes trailing slashes, converts to lowercase).
- Checks for URLs in web apps instead of just IDs.
- Improves exception handling (specific errors and warnings).
- Deeply searches JSON structures (better that just .find()).
- Finds both direct references and indirect ones through web maps.
- Handles errors gracefully without skipping valid results.
- Uses json.dumps() for more flexible searching.
- Improves search range by increasing max_items.
Below is the code:
import arcgis
import pandas as pd
import arcpy
import json
# Log in to portal; 'home' uses the credentials used to login within Pro
gis = arcgis.gis.GIS('home')
# Set up input parameters
find_id = arcpy.GetParameterAsText(0)
search_type = arcpy.GetParameterAsText(1)
# Get the URL of the service and normalize it
find_url = gis.content.get(find_id).url.rstrip('/').lower()
def deep_search(data, search_term):
"""Recursively search for a term in JSON data (dictionaries/lists)."""
if isinstance(data, dict):
return any(deep_search(value, search_term) for value in data.values())
elif isinstance(data, list):
return any(deep_search(item, search_term) for item in data)
elif isinstance(data, str):
return search_term in data.lower()
return False
if search_type == 'Web Map':
arcpy.AddMessage("Searching for Web Maps... This could take a few minutes.")
# Retrieve all web maps (increase max_items)
webmaps = gis.content.search('', item_type='Web Map', max_items=1000)
# Find web maps that contain the service URL
map_list = []
for w in webmaps:
try:
wdata = w.get_data()
if wdata and deep_search(wdata, find_url):
map_list.append(w)
except Exception as e:
arcpy.AddWarning(f"Error processing Web Map {w.id}: {str(e)}")
output = pd.DataFrame([{'title': m.title, 'id': m.id, 'type': m.type} for m in map_list])
arcpy.AddMessage(f"OUTPUT TABLE: \n\n {output}")
elif search_type == 'Web Application':
arcpy.AddMessage("Searching for Web Applications... This could take a few minutes.")
# Retrieve all web apps (use advanced search for larger result sets)
webapps = gis.content.search('', item_type='Application', max_items=1000)
# Find web maps that reference the service URL
webmaps = gis.content.search('', item_type='Web Map', max_items=1000)
matching_webmaps = [w.id for w in webmaps if w.get_data() and deep_search(w.get_data(), find_url)]
# Find web apps that contain either the service URL OR a matching web map
app_list = []
for w in webapps:
try:
wdata = w.get_data()
if wdata:
wdata_str = json.dumps(wdata).lower() # Convert JSON to string for searching
criteria = [
find_url in wdata_str, # Direct service URL reference
any(m in wdata_str for m in matching_webmaps) # Indirect reference through web maps
]
if any(criteria):
app_list.append(w)
except Exception as e:
arcpy.AddWarning(f"Error processing Web Application {w.id}: {str(e)}")
output = pd.DataFrame([{'title': a.title, 'id': a.id, 'type': a.type} for a in app_list])
arcpy.AddMessage(f"OUTPUT TABLE: \n\n {output}")