List All ArcGIS Online Groups an Item is Shared with using the ArcGIS API for Python

Table of Contents

Introduction

The ArcGIS API for Python is a powerful Python library that allows users to interact with and automate tasks in ArcGIS Online (or Portal). The API is excellent for programmatically creating, maintaining, and administering content in ArcGIS Online. Groups play a vital role with how content is shared within an Organisation. It can be difficult to keep track of which Item is shared with what Groups. Thankfully, the ArcGIS API for Python has neat functionality to get that information for you.

arcgis modules

The API provides access to your organisations ArcGIS Online via the GIS class in the gis module. This GIS class is the gateway to ArcGIS Online. 

				
					## import GIS which provides the gateway entry to your AGOL
from arcgis.gis import GIS

				
			

Accessing ArcGIS Online

Our first port of call is to access your ArcGIS Online via the GIS class. There are a handful of ways to achieve access, if you are logged into your ArcGIS Online in ArcGIS Pro you can simply use "home", otherwise, another common way is to provide the ArcGIS Online URL, followed by your username and password.
				
					## Access AGOL
agol = GIS("home")
				
			
				
					## Access AGOL
agol = GIS(
    url = "https://your_organisation.maps.arcgis.com/",
    username = "Your_Username",
    password = "Your_Password"
)
				
			

The old skool way

This is the workflow or the API version 2.3.0 and previous. The Item object shared_with property is deprecated at 2.3.0 and removed as of 3.0.0.  

The shared_with property is a dictionary and we are interested in the “groups” key that has a value that is a list containing group objects that represents the groups the item is shared with.

				
					## access the content item as an item object
item = agol.content.get("ITEM_ID")

## the shared_with returns a dictionary with key-value pairs.
## "everyone" : True/False
## "org" : True/False
## "groups" : [group_objects]
for group_obj in item.shared_with["groups"]:
    print(group_obj.title)
				
			

The new skool way #1

Similar to the old skool way above. This workflow is for the API version 2.3.0 and latter. Version 2.3.0 saw the introduction of the sharing property for an Item object. This sharing property provides access to the SharingManager which gives control to the user with how the item is shared within the Organisation. The SharingManager has a shared_with property that is a dictionary containing two key-value pairs. The keys are groups and level, we are of course interested in the groups key which has a value that is a list containing group objects that represent the groups that the item is shared with.

				
					## access the content item as an item object
item = agol.content.get("ITEM_ID")

## the shared_with returns a dictionary with key-value pairs.
## "groups" : [group_objects]
## "level" : SharingLevel enum object
for group_obj in item.sharing.shared_with["groups"]:
    print(group_obj.title)
				
			

The new skool way #2

This workflow is for the API version 2.3.0 and latter. Version 2.3.0 saw the introduction of the sharing property for an Item object. This sharing property provides access to the SharingManager which gives control to the user with how the item is shared within the Organisation. From that SharingManager there is a groups property, which in-turn provides access to a SharingGroupManager which has a list() method fort listing all the groups that the Item is shared with. What trail!! Have a look at it in the code below.

				
					## access the content item as an item object
item = agol.content.get("ITEM_ID")

## item object -> SharingManager (.sharing)
## SharingManager -> SharingGroupManager (.groups)
## SharingGroupManager list() method
for group_obj in item.sharing.groups.list():
    print(group_obj.title)
				
			

Unlock the full potential of ArcGIS Online by mastering the art of efficient Content Management with the ArcGIS API for Python. In this comprehensive course, you will embark on a journey to streamline your geospatial workflows, enhance data organization, and maximize the impact of your ArcGIS Online platform.

Geospatial Professionals, GIS Analysts, Data Managers, and enthusiasts will discover the power of automation and script-based operations to efficiently manage content in ArcGIS Online. Throughout this course, you will gain practical, hands-on experience in leveraging the ArcGIS API for Python to perform a wide range of content management tasks with ease.

Leave a Comment

Your email address will not be published. Required fields are marked *