Instrução

Automatizar a reatribuição de propriedade de itens do ArcGIS Online compartilhados para grupos com o recurso Atualizar

Last Published: January 15, 2025

Resumo

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

Procedimento

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

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

Note:
Some functions used in this script are only available in ArcGIS API for Python 2.4 and newer versions.
  1. Import the necessary modules and create a connection to the 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 have required privileges to take the ownership of a shared update group, see the article ERROR: Unable to reassign the group at this time for more details):
#Identifying users and signing into AGOL 
current_owner_username = "<Username of the current owner of content and groups>"
new_owner_username = "<Username of the user who will take the ownership of content and groups>"
current_owner = gis.users.get(current_owner_username)
  1. Change ownership of the groups from the current owner to the new owner. If a group is not owned by the current user, the new user will be added to the group instead of reassigning the ownership.
#Transferring the ownership of ALL GROUPS owned by the old user
print("\n***Transferring the ownership of groups...")
usergroups = current_owner.groups
for group in usergroups:
    grp = gis.groups.get(group['id'])
    if (grp.owner == current_owner_username):
        grp.reassign_to(new_owner_username)
    else:
        grp.add_users(new_owner_username)
        grp.remove_users(current_owner_username)
print("The ownership/membership of groups was successfully updated!")
  1. Create copies of the current owner's folders in the new owner content. Un-share the items from the groups, reassign ownership, and re-share the items to the same groups.
#Transferring items in folders        
print("\n***Transferring the ownership of items in folders")
folders = current_owner.folders
num_list=[]
for folder in folders:
    gis.content.folders.create(folder['title'], new_owner_username)
    folderitems = current_owner.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:
        groups=item.sharing.groups.list()
        for group in groups:
            item.sharing.groups.remove(group)
        item.reassign_to(new_owner_username, target_folder=folder['title'])
        for group in groups:
            item.sharing.groups.add(group)
    def sum_list(num_list):
        sum = 0
        for x in num_list:
            sum += x
        return sum
    total_items=sum_list(num_list)

print("\nTransferred ownership of " +str(total_items) + " item(s) in folders.")
  1. Create a folder to move the items from the current owner's root folder in the new owner content. Un-share the items from the groups, reassign ownership, and re-share the items with the same groups.
#Transferring items in the root folder   
print("\n***Transferring the ownership of items in the root folder")
userrootcontent = current_owner.items()
num_root_items=int(len(userrootcontent))
rootfoldername=current_owner.username+"_RootFolder"
gis.content.folders.create(rootfoldername, new_owner_username)

for item in userrootcontent:
    groups=item.sharing.groups.list()
    for group in groups:
        item.sharing.groups.remove(group)
    item.reassign_to(new_owner_username, target_folder=rootfoldername)
    for group in groups:
        item.sharing.groups.add(group)

print("Transferred ownership of "+ str(num_root_items)+" item(s) in Root Folder.")
  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 signing into AGOL  current_owner_username = "<Username of the current owner of content and groups>"
new_owner_username = "<Username of the user who will take the ownership of content and groups>"
current_owner = gis.users.get(current_owner_username)

#Transferring the ownership of ALL GROUPS owned by the old user print("\n***Transferring the ownership of groups...") usergroups = current_owner.groups for group in usergroups:
    grp = gis.groups.get(group['id'])
    if (grp.owner == current_owner_username):
        grp.reassign_to(new_owner_username)
    else:
        grp.add_users(new_owner_username)
        grp.remove_users(current_owner_username)
print("The ownership/membership of groups was successfully updated!")

#Transferring items in folders         print("\n***Transferring the ownership of items in folders") folders = current_owner.folders
num_list=[]
for folder in folders:
    gis.content.folders.create(folder['title'], new_owner_username)
    folderitems = current_owner.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:
        groups=item.sharing.groups.list()
        for group in groups:
            item.sharing.groups.remove(group)
        item.reassign_to(new_owner_username, target_folder=folder['title'])
        for group in groups:
            item.sharing.groups.add(group)
    def sum_list(num_list):
        sum = 0
        for x in num_list:
            sum += x
        return sum
    total_items=sum_list(num_list)

print("\nTransferred ownership of " +str(total_items) + " item(s) in folders.")

#Transferring items in the root folder    print("\n***Transferring the ownership of items in the root folder")
userrootcontent = current_owner.items()
num_root_items=int(len(userrootcontent))
rootfoldername=current_owner.username+"_RootFolder"
gis.content.folders.create(rootfoldername, new_owner_username)

for item in userrootcontent:
    groups=item.sharing.groups.list()
    for group in groups:
        item.sharing.groups.remove(group)
    item.reassign_to(new_owner_username, target_folder=rootfoldername)
    for group in groups:
        item.sharing.groups.add(group)

print("Transferred ownership of "+ str(num_root_items)+" item(s) in Root Folder.")

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

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

ID do Artigo: 000021363

Receba notificações e encontre soluções para problemas novos ou comuns

Obtenha respostas resumidas e soluções de vídeo do nosso novo chatbot de IA.

Baixe o Aplicativo de Suporte da Esri

Informações Relacionadas

Descubra mais sobre este tema

Obtenha ajuda de especialistas do ArcGIS

Entre em contato com Suporte Técnico

Baixe o Aplicativo de Suporte da Esri

Ir para as opções de download
Esri Support AI Chatbot