操作方法

操作方法:向具有更新功能的群组自动重新分配共享的 ArcGIS Online 项目的所有权

Last Published: January 15, 2025

摘要

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.

过程

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: 000021363

接收通知并查找新问题或常见问题的解决方案

从我们全新的 AI 聊天机器人中获得简明答案和视频解决方案。

下载 Esri 支持应用程序

相关信息

发现关于本主题的更多内容

获取来自 ArcGIS 专家的帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项
Esri Support AI Chatbot