Finding dependencies between ArcGIS Online items

1462
3
Jump to solution
09-06-2021 05:29 AM
DonMorrison1
Occasional Contributor III

I am working with a team to develop a HUB site and we are finding it difficult to manage all of the AGOL items that created. One aspect in particular is understanding relationships between items and finding obsolete/unreferenced items.  I wrote a python script that prints a report listing the relationships and wanted to share it - not sure if this is the right place since it isn't really a question but I couldn't figure out how to create a blog entry.  You have to update lines 6 and 7 before running to customize it with your credentials and to query your items.

from arcgis.gis import GIS
import re
import json

# UPDATE THESE 2 STATEMENTS BEFORE RUNNING
AGOL = GIS(username=MY_USER_ID, password=MY_PASSWORD)
CONTENT_QUERY = MY_CONTENT_QUERY   # eg "owner: %s" % 'MY_USER_ID'


REGEX = '"[0-9a-f]{32}"'

def run ():
    
    # Build dicts of all agol items and all item descriptions (indexed by item id)
    all_items = { i.id: i for i  in AGOL.content.search(query = "%s" % CONTENT_QUERY, max_items=5000)}
    all_item_descs = { i: "%s: %s" % (all_items[i].id, all_items[i].title) for i in all_items.keys()}


    # Find the foward references for each item
    item_types = set()    
    item_id_to_forward_refs = {lst: set() for lst in all_items.keys()}     
    for item_id in all_items.keys():
        item_types.add(all_items[item_id].type)
        try:
            item_id_to_forward_refs[item_id].update([d[1:-1] for d in list(set(re.findall(REGEX, json.dumps(all_items[item_id].get_data(True)))))])
        except:
            pass
        for rel_type in ['Map2Service', 'WMA2Code', 'Map2FeatureCollection', 'MobileApp2Code', 'Service2Data', 'Service2Service']:
            item_id_to_forward_refs[item_id].update([i.id for i in all_items[item_id].related_items(rel_type, 'forward')])
           

    # Find the backward references for each item        
    item_id_to_backward_refs = {lst: [] for lst in all_items.keys()}
    for item_id in all_items.keys():
        for i in all_items.keys():
            if i in item_id_to_forward_refs[item_id]:
                item_id_to_backward_refs[i].append(item_id)


    # Print out the results
    for item_type in item_types:
        print ("\n\n========================%ss================================" % (item_type))
        print ("Unreferenced")
        unreferenced_item_ids = [i for i in item_id_to_backward_refs.keys() if all_items[i].type == item_type and len(item_id_to_backward_refs[i]) == 0]
        print ('\n'.join( [all_item_descs[i] for i in unreferenced_item_ids] ))
        print ("\n--------------------------------------------------------------------" )
        print ("Referenced")
        referenced_item_ids = [i for i in item_id_to_backward_refs.keys() if all_items[i].type == item_type and len(item_id_to_backward_refs[i]) > 0]
        for referenced_item_id in referenced_item_ids:
            print ("%s" % all_item_descs[referenced_item_id])
            print ('\t' + '\n\t'.join( [all_item_descs[r] for r in item_id_to_backward_refs[referenced_item_id]] ))


if __name__ == '__main__':
    run()

 

Tags (3)
1 Solution

Accepted Solutions
David_Brooks
MVP Regular Contributor

@DonMorrison1 really nice work Don. I can see that being useful to a lot of GIS Managers


David
..Maps with no limits..

View solution in original post

0 Kudos
3 Replies
David_Brooks
MVP Regular Contributor

@DonMorrison1 really nice work Don. I can see that being useful to a lot of GIS Managers


David
..Maps with no limits..
0 Kudos
IvesJH_JCGIS
New Contributor

@DonMorgan Thank you for putting this together. I am trying to use it however I am new to python and am having issues with this running. I have it in Python Notebook (on Pro). I am having issues with Line 7. Currently, I have: CONTENT_QUERY = "owner: MeAs_Owner"

Is this what the Content Query is after? The Member user name?

Thanks for your time,

Jace

0 Kudos
DonMorrison1
Occasional Contributor III

That string, when used in the query on line 15, should return information on all AGOL items owned by the specified user. 

0 Kudos