HOW TO

Automate ownership reassignment of ArcGIS Online items shared to groups with the Update capability

Last Published: December 9, 2021

Summary

When reassigning ownership of items in ArcGIS Online, a prerequisite step is to unshare items from special groups, such as those with the Update capability for all members, before changing or reassigning ownership. This is time-consuming when reassigning ownership of multiple items shared to the Update groups. It is therefore helpful to automate the process using a Python script.

Procedure

To automate the workflow, use the following Python script. This article outlines the steps in the script.

This script can be run in Jupyter Notebook, ArcGIS Notebook, or as a stand-alone script in your Python environment.

  1. Import the necessary modules and create a connection to your Portal by defining the URL and credentials for a user with administrative privileges.
from arcgis.gis import *
import itertools
gis = GIS("https://<shortname>.maps.arcgis.com/", "<admin_username>", "<admin_password>")
  1. Identify the usernames for the current owner and the user that will take the ownership of items and groups (both users must be admins to take the ownership of a shared update group, see this article for more details):
#Identifying users and siging into AGOL (Both users need to have Administrator role)
orig_userid = "<Username of the current owner of content and groups>"
new_userid = "<Username of the user who will take the ownership of content and groups>"
olduser = gis.users.get(orig_userid)
  1. Change ownership of the old user groups to the new user.
#Transferring the ownership of ALL GROUPS owned by the old user
print("\n***Transferring the ownership of groups...")
usergroups = olduser.groups
for group in usergroups:
    grp = gis.groups.get(group['id'])
    if (grp.owner == orig_userid):
        grp.reassign_to(new_userid)
    else:
        grp.add_users(new_userid)
        grp.remove_users(orig_userid)

print("The ownership of groups was successfully transferred!")
  1. Create copies of old user folders in the new user content. Un-share the items from the groups, reassign ownership, and re-share the items to the their original sharing status.
#Transferring items in folders        
print("\n***Transferring the ownership of items in selected folders...")

folders = olduser.folders
num_list=[]
for folder in folders:
    gis.content.create_folder(folder['title'], new_userid)
    folderitems = olduser.items(folder=folder['title'])
    num_items=int(len(folderitems))
    num_list.append(num_items)
    print("Transferring "+ str(num_items) + " item(s) from folder: "+folder['title'])
    for item in folderitems:
        itemsharing=item.shared_with
        item.unshare(groups=itemsharing['groups'])
        item.reassign_to(new_userid, target_folder=folder['title'])
        item.share(everyone=itemsharing['everyone'], org=itemsharing['org'], groups=itemsharing['groups'], allow_members_to_edit=True)

    def sum_list(num_list):
        sum = 0
        for x in num_list:
            sum += x
        return sum
    total_items=sum_list(num_list)

print("\nThe ownership of " +str(total_items) + " items was successfully transferred!")
  1. Create a folder to move the items from the old user's root folder in the new user content. Un-share the items from the groups, reassign ownership, and re-share the items to the their original sharing status.
#Transferring items in the root folder   
print("\n***Transferring the ownership of items in the root folder...")
userrootcontent = olduser.items()
num_root_items=int(len(userrootcontent))
rootfoldername=olduser.username+"_RootFolder"
gis.content.create_folder(rootfoldername, new_userid)
root_groups_list=[]

for item in userrootcontent:
    rootitemsharing=item.shared_with
    root_group=rootitemsharing['groups']
    root_groups_list.append(root_group)
    valueToBeRemoved = []
    try:
        while True:
            root_groups_list.remove(valueToBeRemoved)
    except ValueError:
        pass

    root_groups= list(itertools.chain(*root_groups_list))

    for group in root_groups:
        grop = gis.groups.get(group['id'])
        if (grop.owner == orig_userid):
            grop.reassign_to(new_userid)
        else:
            grop.add_users(new_userid)
            grop.remove_users(orig_userid)
    item.unshare(groups=rootitemsharing['groups'])
    item.reassign_to(new_userid, target_folder=rootfoldername)
    item.share(everyone=rootitemsharing['everyone'], org=rootitemsharing['org'], groups=rootitemsharing['groups'], allow_members_to_edit=True)

print("The ownership of "+ str(num_root_items)+" items in Root Folder was transferred successfully!")
  1. Print the total number of items that were successfully transferred to the new user.
print ("\nTotal number of items transferred: "+str(total_items+num_root_items)+" items") 

The following demonstrates the full script.

from arcgis.gis import *
import itertools
gis = GIS("https://<shortname>.maps.arcgis.com/", "<admin_username>", "<admin_password>")

#Identifying users and siging into AGOL (Both users need to have Administrator role)
orig_userid = "<Username of the current owner of content and groups>"
new_userid = "<Username of the user who will take the ownership of content and groups>"
olduser = gis.users.get(orig_userid)

#Transferring the ownership of ALL GROUPS owned by this user
print("\n***Transferring the ownership of groups...")
usergroups = olduser.groups
for group in usergroups:
    grp = gis.groups.get(group['id'])
    if (grp.owner == orig_userid):
        grp.reassign_to(new_userid)
    else:
        grp.add_users(new_userid)
        grp.remove_users(orig_userid)

print("The ownership of groups was successfully transferred!")

#Transferring items in folders        
print("\n***Transferring the ownership of items in selected folders...")

folders = olduser.folders
num_list=[]
for folder in folders:
    gis.content.create_folder(folder['title'], new_userid)
    folderitems = olduser.items(folder=folder['title'])
    num_items=int(len(folderitems))
    num_list.append(num_items)
    print("Transferring "+ str(num_items) + " item(s) from folder: "+folder['title'])
    for item in folderitems:
        itemsharing=item.shared_with
        item.unshare(groups=itemsharing['groups'])
        item.reassign_to(new_userid, target_folder=folder['title'])
        item.share(everyone=itemsharing['everyone'], org=itemsharing['org'], groups=itemsharing['groups'], allow_members_to_edit=True)

    def sum_list(num_list):
        sum = 0
        for x in num_list:
            sum += x
        return sum
    total_items=sum_list(num_list)

print("\nThe ownership of " +str(total_items) + " items was successfully transferred!")
            
#Transferring items in the root folder   
print("\n***Transferring the ownership of items in the root folder...")
userrootcontent = olduser.items()
num_root_items=int(len(userrootcontent))
rootfoldername=olduser.username+"_RootFolder"
gis.content.create_folder(rootfoldername, new_userid)
root_groups_list=[]

for item in userrootcontent:
    rootitemsharing=item.shared_with
    root_group=rootitemsharing['groups']
    root_groups_list.append(root_group)
    valueToBeRemoved = []
    try:
        while True:
            root_groups_list.remove(valueToBeRemoved)
    except ValueError:
        pass

    root_groups= list(itertools.chain(*root_groups_list))

    for group in root_groups:
        grop = gis.groups.get(group['id'])
        if (grop.owner == orig_userid):
            grop.reassign_to(new_userid)
        else:
            grop.add_users(new_userid)
            grop.remove_users(orig_userid)
    item.unshare(groups=rootitemsharing['groups'])
    item.reassign_to(new_userid, target_folder=rootfoldername)
    item.share(everyone=rootitemsharing['everyone'], org=rootitemsharing['org'], groups=rootitemsharing['groups'], allow_members_to_edit=True)

print("The ownership of "+ str(num_root_items)+" items in Root Folder was transferred successfully!")

print ("\nTotal number of items transferred: "+str(total_items+num_root_items)+" items") 

print("\n***Process was completed!***")

Article ID: 000021363

Software:
  • ArcGIS Online
  • 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