Possible to find out where feature layers are being used in web applications?

15554
41
Jump to solution
02-09-2022 08:12 AM
ZachRobinson_SantaClaraCounty
New Contributor II

I am wondering if it is possible to easily find out which web applications in our ArcGIS Online organization are using a specific feature layer.

For example, if I delete a layer from our organization, I want to make sure I know which web maps and applications the deletion will affect. This can be difficult and tedious to search manually when an organization has many web maps and applications. 

41 Replies
lzk222
by
New Contributor III

I've not encountered this problem. I'd try using a VPN and running this script. If you're already using one, try running it with the VPN turned off.

0 Kudos
lzk222
by
New Contributor III

I've used this script to great effect over the years. Thanks to all who have contributed to it. I just wanted to post my slightly improved version that includes Web Experiences (kudos @Big-Chill), Web Maps, better exception handling, and hyperlinks the titles (in Jupyter Notebooks). 

Output should look something like this:

lzk222_0-1699575237003.png

 

from arcgis.gis import GIS
import pandas as pd
from IPython.display import display, HTML

# Log in to portal; prompts for PW automatically
gis = GIS('your-portal-url', 'username')

# Layer ID to search for
find_id = 'a405b06ae8e24f94a2768a4581b79e73'

# Get the content based on ID
content_item = gis.content.get(find_id)

# Check if content is found
if content_item:
    find_url = content_item.url

    # URL snippet for web maps e.g. https://osit.maps.arcgis.com/apps/mapviewer/index.html?webmap=
    web_map_url_snippet = 'your-webmap-domain-path'

    # Pull list of all web maps in portal
    webmaps = gis.content.search('', item_type='Web Map', max_items=-1)

    # Return subset of map IDs which contain the service URL we're looking for
    matches = [m.id for m in webmaps if m.url and find_url in m.url]

    # Create empty list to populate with results
    app_list = []
    exp_list = []
    web_map_list = []  # New list for web maps

    # Pull list of all web apps in portal
    webapps = gis.content.search('', item_type='Application', max_items=-1)

    # Check each web app for matches
    for w in webapps:
        try:
            # Get the JSON as a string
            wdata = str(w.get_data())

            criteria = [
                wdata.find(find_url) > -1,  # Check if URL is directly referenced
                any([wdata.find(i) > -1 for i in matches])  # Check if any matching maps are in app
            ]

            # If layer is referenced directly, append app to 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

    # Pull list of all web experience apps
    webexp = gis.content.search('', item_type='Web Experience', max_items=-1)

    # Check each web experience app for matches
    for wx in webexp:
        try:
            # Get the JSON as a string
            wxdata = str(wx.get_data())

            criteria = [
                wxdata.find(find_url) > -1,  # Check if URL is directly referenced
                any([wxdata.find(i) > -1 for i in matches])  # Check if any matching maps are in app
            ]

            # If layer is referenced directly, append app to list
            if any(criteria):
                exp_list.append(wx)

        # Some apps don't have data, so we'll just skip them if they throw a TypeError
        except:
            continue

    # Check each web map for matches
    for wm in webmaps:
        try:
            # Get the JSON as a string
            wmdata = str(wm.get_data())

            criteria = [
                wmdata.find(find_url) > -1,  # Check if URL is directly referenced
                any([wmdata.find(i) > -1 for i in matches])  # Check if any matching maps are in app
            ]

            # If layer is referenced directly, append web map to list
            if any(criteria):
                # Construct the complete web map URL
                wm_url = f'{web_map_url_snippet}{wm.id}'
                web_map_list.append({'Title': f'<a href="{wm_url}" target="_blank">{wm.title}</a>', 'ID': wm.id, 'Type': 'Web Map'})

        # Some web maps don't have data, so we'll just skip them if they throw a TypeError
        except:
            continue

    # Create DataFrames
    app_df = pd.DataFrame([
        {'Title': f'<a href="{a.url}" target="_blank">{a.title}</a>', 'ID': a.id, 'Type': a.type}
        for a in app_list
    ])

    exp_df = pd.DataFrame([
        {'Title': f'<a href="{b.url}" target="_blank">{b.title}</a>', 'ID': b.id, 'Type': b.type}
        for b in exp_list
    ])

    web_map_df = pd.DataFrame(web_map_list)

    # Display the HTML content
    display(HTML(web_map_df.to_html(escape=False)))
    display(HTML(app_df.to_html(escape=False)))
    display(HTML(exp_df.to_html(escape=False)))

else:
    print(f"Content with ID '{find_id}' not found.")

 

Katie_Clark
MVP Regular Contributor

Wow, thank you for your contributions, @lzk222 !! 🙂

Best,
Katie


“The goal is not simply to ‘work hard, play hard.’ The goal is to make our work and our play indistinguishable.”
- Simon Sinek
JoshuaClanton
New Contributor III

Wow thank you for this! Great script

0 Kudos
TobyStewart2
New Contributor II

That looks amazing @lzk222 

I am trying to run it as a script in an ArcPro toolbox similar to how @Katie_Clark had set it up in her previous post. It runs without errors but there is no output at the end. Im not very good with scripting so am unsure what Im missing.

Thanks again

TobyStewart2_0-1699848175437.png

 

0 Kudos
lzk222
by
New Contributor III

Hi @TobyStewart2 , I am not that familiar with running scripts from the Toolbox. Because the script will return hyperlinks, it is probably best to run it in Jupyter Notebooks since this is browser based. Here's a video on how to get that started: Installing Jupyter Notebooks.

MichaelRobb2
New Contributor II

Hi,

 

This sounds like an ideal solution for us as we have a tidy up of our AGOL resources.  Having forgotten about so much work we've done in the past, it is so difficult to ascertain what web maps, applications etc reference what shapefiles, feature layers and so on.  

Can I ask where the best place to actually run this Python script is i.e Python Notebook within ArcPro?  What form should the 'your-portal-url' be in, should it be "https://<organisation>.maps.arcgis/com/" ?

I've tried running it in a Notebook and after a fair while just get a connection error below.  It is possibly a firewall issue, it has been known!

MichaelRobb2_1-1699892489874.png

Many thanks,

Mike

 

0 Kudos
lzk222
by
New Contributor III

Hi @MichaelRobb2 , 

The best place to run the script is in Jupyter Notebooks

The portal-url should look something like this:

https://osit.maps.arcgis.com/apps/mapviewer/index.html?webmap=


You can find it from any webmap in your organization.

lzk222_0-1700334265329.png

Please try running it from here. If you're still getting errors let me know, though what you've posted appears to be firewall related.

MichaelRobb2
New Contributor II

Many thanks.  I ran the Python script in Jupyter notebooks from home and it worked, so must be an organisational firewall thing indeed.  It's not the first time I've had problems!  

0 Kudos
Jesse_Albers
New Contributor II

@lzk222Thank you for your post. I have noticed a discrepancy when searching for a layer that should appear in a web application. Despite the layer being present in the web map when using this Python tool, it does not appear in the web application that uses the same web map. However, the same layer appears in another web map and web application that uses that web map. Any clue why this would happen? I can post a screenshot if that would help.

0 Kudos