<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Finding dependencies between ArcGIS Online items in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1316595#M68362</link>
    <description>&lt;P&gt;That string, when used in the query on line 15, should return information on all AGOL items owned by the specified user.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Aug 2023 19:39:09 GMT</pubDate>
    <dc:creator>DonMorrison1</dc:creator>
    <dc:date>2023-08-08T19:39:09Z</dc:date>
    <item>
      <title>Finding dependencies between ArcGIS Online items</title>
      <link>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1095732#M62245</link>
      <description>&lt;P&gt;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.&amp;nbsp; 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.&amp;nbsp; You have to update lines 6 and 7 before running to customize it with your credentials and to query your items.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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]) &amp;gt; 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()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 12:29:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1095732#M62245</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2021-09-06T12:29:28Z</dc:date>
    </item>
    <item>
      <title>Re: Finding dependencies between ArcGIS Online items</title>
      <link>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1095733#M62246</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/192850"&gt;@DonMorrison1&lt;/a&gt;&amp;nbsp;really nice work Don. I can see that being useful to a lot of GIS Managers&lt;/P&gt;</description>
      <pubDate>Mon, 06 Sep 2021 12:37:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1095733#M62246</guid>
      <dc:creator>David_Brooks</dc:creator>
      <dc:date>2021-09-06T12:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Finding dependencies between ArcGIS Online items</title>
      <link>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1316579#M68361</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/76591"&gt;@DonMorgan&lt;/a&gt;&amp;nbsp;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"&lt;/P&gt;&lt;P&gt;Is this what the Content Query is after? The Member user name?&lt;/P&gt;&lt;P&gt;Thanks for your time,&lt;/P&gt;&lt;P&gt;Jace&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 19:05:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1316579#M68361</guid>
      <dc:creator>IvesJH_JCGIS</dc:creator>
      <dc:date>2023-08-08T19:05:26Z</dc:date>
    </item>
    <item>
      <title>Re: Finding dependencies between ArcGIS Online items</title>
      <link>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1316595#M68362</link>
      <description>&lt;P&gt;That string, when used in the query on line 15, should return information on all AGOL items owned by the specified user.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 19:39:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/finding-dependencies-between-arcgis-online-items/m-p/1316595#M68362</guid>
      <dc:creator>DonMorrison1</dc:creator>
      <dc:date>2023-08-08T19:39:09Z</dc:date>
    </item>
  </channel>
</rss>

