English

How To: Transfer content from one ArcGIS Online organization or Portal for ArcGIS to another

Summary

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.

Procedure

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.

  1. Import the necessary module.
from arcgis.gis import GIS
  1. Specify the URL and credential of the origin ArcGIS Online account. 
#For ArcGIS Online
gis=GIS("https://arcgis.com", "Username", "Password")

#For Portal for ArcGIS
gis=GIS("https://<gisserver>.<domain>.com/portal", "Username", "Password")
  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("https://arcgis.com", username_2)

#For Portal for ArcGIS
gis2 = GIS("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), 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), 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 = gis.content.get(itemid)
  • Use the Search function with a query to search for specific items to clone. The general format is as follows:
items = gis.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.  
def deep_copy_content(input_list):
    for item in input_list:
        try:
            print("Cloning " + item.title)
            copy_list = []
            copy_list.append(item)
            gis2.content.clone_items(copy_list, copy_data=True, search_existing_items=True)
            print("Successfully cloned " + item.title)
        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

gis=GIS("https://arcgis.com", "Username", "Password")

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

#Get a specific item using item id:
itemid = 'cc94b27a35d14f40987d96f3d2a39e67'
items = gis.content.get(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)
            gis2.content.clone_items(copy_list, copy_data=True, search_existing_items=True)
            print("Successfully cloned " + item.title)
        except Exception as e:
            print(e)
    print("The function has completed")
deep_copy_content(items)

Related Information

Last Published: 2/18/2021

Article ID: 000022252

Software: Portal for ArcGIS 10.8.1, 10.8, 10.7.1, 10.7, 10.6.1, 10.6 ArcGIS Online 8.3