English

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

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 stand-alone Python script:

  1. Open a text editor, such as Notepad.
  2. In the text editor, import the necessary modules and define the location of the items to be reassigned.
from arcgis.gis import *
gis = GIS("https://python.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")
  1. Add the following parameters to reassign the ownership:
orig_userid = "First_User" 
new_userid = "Second_User" 
olduser = gis.users.get(orig_userid)
  1. To change ownership of groups, add the following parameters. Or, comment out the parameter by adding the # symbol at the start of the line.
usergroups = olduser.groups
  1. Add the following parameters to change ownership of the old user groups to a new user:
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)

usercontent = olduser.items()
  1. Add the following parameters to unshare the items from the groups and reassign ownership:
for item in usercontent:
item.unshare(usergroups)
try:
item.reassign_to(new_userid)
except Exception as e:
print("Item may have been already assigned to the user.")
  1. Once the script is completed, save the text file as a .py file.
  2. Double click the .py file to run the script.

Below is the full script.

from arcgis.gis import *
gis = GIS("https://python.playground.esri.com/portal", "arcgis_python", "amazing_arcgis_123")

orig_userid = "First_User"
new_userid = "Second_User"
olduser = gis.users.get(orig_userid)

#If you need to change ownership of groups as well.
usergroups = olduser.groups

#Change the ownership of old user groups to a new user.
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)

usercontent = olduser.items()

#Unshare the items from the groups and reassign ownership.
for item in usercontent:
   item.unshare(usergroups)
   try:
      item.reassign_to(new_userid)
   except Exception as e:
      print("Item may have been already assigned to the user.")

Related Information