English

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

Summary

It is sometimes thought that ArcGIS Online Assistant is the only way to copy contents from one ArcGIS Online organization to another. However, the instructions provided below describe how to do this using the ArcGIS API for Python to copy contents from ArcGIS Online. This includes Web Maps, Configurable Apps, Dashboards, Shapefiles, File Geodatabases, etc.

Procedure

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

  1. Connect to the origin ArcGIS Online organization. Import GIS from arcgis.gis and provide login information.
from arcgis.gis import GIS

You can embed your credentials in the script provide them as parameters, or get username and password as input parameters:

  • Option 1: Add your username and password to avoid typing it in every time:
gis=GIS("https://arcgis.com", 'Your Username', 'Your Password')
  • Option 2: Provide username and password as parameters:
Username = "your username"
Password = "your password"
  • Option 3: Get username and password as input parameters:
Username = input("Enter username of origin organization: ")
gis1 = GIS("https://arcgis.com", username)
  1. After returning this section of code, a prompt to enter the account password is returned. Run the cell to connect to the target ArcGIS Online account.
  2. Add the following code section to create the account that we will clone content into (with the same options as mentioned above):
username_2 = input("Enter username of target organization: ")
gis2 = GIS("https://arcgis.com", username_2)

A prompt appears to enter the password for the target ArcGIS Online account.

  1. Create a list of items to clone. To do this, the search function with a query can be used. You can pass on the maximum number of items as a parameter for the search. There is the option to set this number in the script or get it as an input.
  • Option 1: Set the maximum number of items to export:
num_items = 5
  • Option 2: Get maximum number of items as input parameter:
num_items = int(input("How many items would you like to clone? ")) 
  • To clone a specific item use the Get function:

Get an item using item id:

itemid = 'cc94b27a35d14f40987d96f3d2a39e67'
items = gis.content.get(itemid) 

Alternatively, use the Search function with a query to search for specific items to clone. The general format is:

#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)

For more examples of using queries in the Search function to find items to clone, refer to the article: How To: Find specific items using queries in the arcgis.gis module with the ArcGIS Python API

Currently, the query is set up to find a specific number of items in username's content. See the ArcGIS API for Python documentation on the search function for information on how to modify the search.

items = gis1.content.search(query="owner: {}".format(username), max_items=num_items, sort_field='id', sort_order='desc')

The code below returns an informational message about how many items will be cloned, and the list of items.

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

The list is used as input into the clone_items function. Some logic is added to the example here to handle errors if an item cannot be cloned (for example, referenced ArcGIS Enterprise services cannot be cloned). The ArcGIS API for Python clone_items function instantiates a new hosted feature layer in the target organization when copy_data=True, which is shown below. The function also accounts for dependencies. For example, if a web map with two layers is cloned and copy_data=True, the two feature layers in the web map are created in the target organization and the paths in the cloned web map will be swizzled to point at the new layers. Dependencies are also accounted for with surveys, dashboards, and other web apps. See the full list of parameters for clone_items, such as folder to specify a destination folder, in the linked documentation above, and optionally modify the clone_items() function below.

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)

The following shows the full script:

from arcgis.gis import GIS
username = input("Enter username of origin organization: ")
gis1 = GIS("https://arcgis.com", username)

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

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') 
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: 11/20/2020

Article ID: 000022252

Software: ArcGIS Online 8.2