HOW TO
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.
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.
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 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!")
#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")
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!***")
Get help from ArcGIS experts
Download the Esri Support App