English

How To: Find dependencies in Portal for ArcGIS

Summary

In some cases, it is necessary to determine the dependencies between Portal for ArcGIS items. For example in data cleanup, a web app and its associated web map may need to be deleted. Manually checking these dependencies can be tedious. As an alternative, this process can be performed programmatically with the Administrator privilege using the following ArcGIS API for Python functions: dependent_upon() and dependent_to().

Procedure

The following steps describe how to find dependencies in Portal for ArcGIS using the ArcGIS API for Python.

  1. Import the necessary modules.
from arcgis.gis import GIS
from IPython.display import display
  1. Specify the source URL and the login credentials for Portal for ArcGIS.
source = GIS("<portalurl>", "<admin_user>", "<admin_password>")
source_users = source.users.search(query=None, sort_field='username', sort_order='asc', max_users=100, outside_org=False, exclude_system=True)
arcpy.AddMessage(source_users)
print('User list: Role')
  1. Create a loop to search for all usernames in the portal.
for user in source_users:
    print(user.username + "\t:\t" + str(user.role))
    arcpy.AddMessage(user.username + "\t:\t" + str(user.role))
  1. Create a loop to collect the Item IDs in the available folders.
for user in source_users:
    num_items = 0
    num_folders = 0
    print("Collecting item ids for {}".format(user.username), end="\n\n")
    arcpy.AddMessage("Collecting item ids for {}".format(user.username))
    user_content = user.items()
  1. In the same loop, retrieve the Item IDs from the root folder and check for dependencies.
    for item in user_content:
        num_items += 1
        source_items_by_id[item.itemid] = item 
        print("\n\n Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
        arcpy.AddMessage('')
        arcpy.AddMessage("Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
        if (item.dependent_to()['total'] > 0):
            print("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
            arcpy.AddMessage("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
  1. In the same loop, retrieve the Item IDs from the subfolders and check for dependencies.
    folders = user.folders
    for folder in folders:
        num_folders += 1
        folder_items = user.items(folder=folder['title'])
        for item in folder_items:
            num_items += 1
            source_items_by_id[item.itemid] = item
            print("\n\n Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
            arcpy.AddMessage('')
            arcpy.AddMessage("Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
            if (item.dependent_to()['total'] > 0):
                print("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
                arcpy.AddMessage("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
    
    print("Number of folders {} # Number of items {} # Number of users {}".format(str(num_folders), str(num_items), len(source_users)))

The following shows the full script:

from arcgis.gis import GIS
from IPython.display import display

source = GIS("https://test.domain.com/portal", "portaladmin", "portaladmin")
source_users = source.users.search(query=None, sort_field='username', sort_order='asc', max_users=100, outside_org=False, exclude_system=True)
arcpy.AddMessage(source_users)
print('User list: Role')

for user in source_users:
    print(user.username + "\t:\t" + str(user.role))
    arcpy.AddMessage(user.username + "\t:\t" + str(user.role))

for user in source_users:
    num_items = 0
    num_folders = 0
    print("Collecting item ids for {}".format(user.username), end="\n\n")
    arcpy.AddMessage("Collecting item ids for {}".format(user.username))
    user_content = user.items()
    
    for item in user_content:
        num_items += 1
        source_items_by_id[item.itemid] = item 
        print("\n\n Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
        arcpy.AddMessage('')
        arcpy.AddMessage("Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
        if (item.dependent_to()['total'] > 0):
            print("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
            arcpy.AddMessage("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
            
    
    folders = user.folders
    for folder in folders:
        num_folders += 1
        folder_items = user.items(folder=folder['title'])
        for item in folder_items:
            num_items += 1
            source_items_by_id[item.itemid] = item
            print("\n\n Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
            arcpy.AddMessage('')
            arcpy.AddMessage("Item: \t {2} \t\t\t id {0} is dependent on these items: \t {1}".format(item.itemid,item.dependent_upon(), item.title))
            if (item.dependent_to()['total'] > 0):
                print("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
                arcpy.AddMessage("\t This item is also a dependency of these items: {}".format(item.dependent_to()))
    
    print("Number of folders {} # Number of items {} # Number of users {}".format(str(num_folders), str(num_items), len(source_users)))

Related Information