CÓMO

Copiar/clonar contenido de una organización de ArcGIS Online o Portal for ArcGIS a otra

Last Published: October 22, 2024

Resumen

ArcGIS Online Assistant is often considered as the only way to copy contents from one ArcGIS Online organization or Portal for ArcGIS to another. However, the instructions provided below describe how to do this using the ArcGIS API for Python to copy contents web maps, configurable apps, dashboards, shapefiles, file geodatabases, and others.

Procedimiento

The following Python script (run from a Jupyter Notebook interface) uses the clone_items method in the arcgis module to copy hosted feature layers and the underlying service from one ArcGIS Online organization to another. The sample cells used are intended for use in Jupyter Notebook, but can be modified for use in a standalone script.

Note:
The user executing the copy should have necessary privileges to access the items in the source GIS and to create items in the the target GIS.

Using administrator accounts guarantees all necessary privileges within the source to access the item and any dependencies, and within the target to create the services and items.
  1. Import the necessary module.
from arcgis.gis import GIS
  1. Specify the URL and credential of the origin ArcGIS Online account. 
username_1 = input("Enter username of source organization: ")

#For ArcGIS Online
gis1=GIS(r"https://arcgis.com", username_1)

#For Portal for ArcGIS
gis1=GIS(r"https://<gisserver>.<domain>.com/portal", username_1)
  1. Specify the username of the account to export the cloned items. 
username_2 = input("Enter username of target organization: ")

#For ArcGIS Online
gis2 = GIS(r"https://arcgis.com", username_2)

#For Portal for ArcGIS
gis2 = GIS(r"https://<gisserver>.<domain>.com/portal", username_2)
  1. Create a list of items to clone. There are a few options to do so:
  • Set the maximum number of items to export:
num_items = 5
items = gis1.content.search(query="owner: {}".format(username_1), max_items=num_items, sort_field='id', sort_order='desc')
  • Get the maximum number of items as input parameter:
num_items = int(input("How many items would you like to clone? "))
items = gis1.content.search(query="owner: {}".format(username_1), max_items=num_items, sort_field='id', sort_order='desc')
  • Get a specific item using item id:
itemid = '<item_ID>' #insert the item id
items = gis1.content.search(itemid)
  • Use the Search function with a query to search for specific items to clone. The general format is as follows:
items = gis1.content.search(query, item_type=None, sort_field='avgRating', sort_order='desc', max_items=10, outside_org=False, categories=None, category_filters=None)
  1. Print the list of the items to be cloned.
print(str(len(items)) + " items will be cloned. See the list below:") 
items
  1. Create a logic to clone the items using the clone_items() function and to handle an error if the cloning process fails or if there is an existing item with the same name in the target, which prevents cloning the item if search_existing_items is set to True:
def deep_copy_content(input_list):
    for item in input_list:
        try:
            print("Cloning " + item.title)
            copy_list = []
            copy_list.append(item)
            clone_results=gis2.content.clone_items(copy_list, copy_data=True, search_existing_items=True)
            if len(clone_results) > 0:
                print("  > "+item.title + " was successfully cloned.")
            else:
                print("  > "+item.title + " was not cloned. An item with the same name may exist in the destination org.")
        except Exception as e:
            print(e)
    print("The function has completed.")
deep_copy_content(items)
Note:
As an alternative, the clone_items() function can also be used outside of the logic code block if handling exception is unnecessary. After step 5, the clone_items() function can be used as such:

gis2.content.clone_items([items])

The following shows a sample of a full script:

from arcgis.gis import GIS

username_1 = input("Enter username of source organization: ")
gis1=GIS(r"https://arcgis.com", username_1)

username_2 = input("Enter username of target organization: ")
gis2 = GIS(r"https://arcgis.com", username_2)

#Get a specific item using item id:
itemid = 'cc94b27a35d14f40987d96f3d2a39e67'
items = gis1.content.search(itemid) 

print(str(len(items)) + " items will be cloned. See the list below:") 
items

def deep_copy_content(input_list):
    for item in input_list:
        try:
            print("Cloning " + item.title)
            copy_list = []
            copy_list.append(item)
            clone_results=gis2.content.clone_items(copy_list, copy_data=True, search_existing_items=True)
            if len(clone_results) > 0:
                print("  > "+item.title + " was successfully cloned.")
            else:
                print("  > "+item.title + " was not cloned. An item with the same name may exist in the destination org.")
        except Exception as e:
            print(e)
    print("The function has completed.")
deep_copy_content(items)

Id. de artículo: 000022252

Recibir notificaciones y encontrar soluciones a problemas nuevos o comunes

Obtenga respuestas resumidas y soluciones de vídeo de nuestro nuevo chatbot de IA.

Descargar la aplicación de soporte de Esri

Información relacionada

Descubrir más sobre este tema

Obtener ayuda de expertos en ArcGIS

Contactar con el soporte técnico

Descargar la aplicación de soporte de Esri

Ir a opciones de descarga