HOW TO

Create a list of groups that a service is shared with in Portal for ArcGIS using ArcGIS API for Python

Last Published: July 19, 2023

Summary

There is no built-in functionality to get the list of groups that an item is shared with. In some cases where a portal is required to be re-federated, it can be tedious when the list of groups must be retrieved manually. The article provides an alternative way by using an ArcGIS API for Python script to retrieve the list for each item by specifying the itemID.

Procedure

  1. Import the necessary modules and specify the credentials.
from arcgis.gis import GIS
gis=GIS("https://www.arcgis.com","UserName","Password", verify_cert=False)
  1. Get all the groups from the specified user and specify the item ID.
groups = gis.groups.search(query = "owner:UserName")

Feature = 'd3a411e5beb7479e8c810671886094a1'

groups
group = []
  1. Call a dictionary function to store the collection of results.
d2=dict()
  1. Create a loop to search for all group names that the item is shared to and store it to an array.
for g in groups:
    #print(g.title)
    item = g.content()
    l1=[]
    for thing in item:
        l1.append(thing.itemid)
    d2[g.title]=l1

search_results = []
  1. Create a loop to store the search result in the dictionary and print the list.
for key, item in d2.items():
    if Feature in item:
        search_results.append(key)
        
print(search_results)

The following is a sample of the full script:

from arcgis.gis import GIS
gis=GIS("https://www.arcgis.com","UserName","Password", verify_cert=False)

groups = gis.groups.search(query = "owner:UserName")

Feature = 'd3a411e5beb7479e8c810671886094a1'

groups
group = []

d2=dict()

for g in groups:
    #print(g.title)
    item = g.content()
    l1=[]
    for thing in item:
        l1.append(thing.itemid)
    d2[g.title]=l1

search_results = []

for key, item in d2.items():
    if Feature in item:
        search_results.append(key)
        
print(search_results)

Article ID: 000018007

Software:
  • Portal for ArcGIS
  • ArcGIS API for Python 1 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options