GIS

AGOL Migration Script by Robert Walker

ESRI recently released their ArcGIS Python API v1.3.  In the release post ESRI highlights the clone_items() method of the ContentManager class.  Given that we recently merged several accounts and had a new central ArcGIS Online (AGOL) domain I found this method super helpful.  We have dozens of apps, maps, and feature layers to move.  This single method made it a one-script process!  Just feed it the ID(s) of an App or Map and it will migrate all of the content required to make it work.

Hopefully to save you some time, you can copy the script below.  Just fill in the "blanks" and run it.

"""
Script to migrate AGOL items to another AGOL.  Requires ArcGIS Python API 1.3+

Author: Dylan Walker
Date: 12/25/2017

"""
import json

from arcgis import gis

FOLDER_TO_PUT = 'THE FOLDER TO SAVE ITEMS TO (OPTIONAL)'  # Folder on TO_GIS to put items into, optional
FROM_GIS_URL = 'THE *FROM* AGOL OR PORTAL URL'  # URL of the GIS Portal/AGOL to copy FROM
TO_GIS_URL = 'THE *TO* AGOL OR PORAL URL'  # URL of the GIS Portal/AGOL to copy TO

# Passwords JSON file - make this your own; username is the key, password is the value 
# (optional, you can hard-code the passwords if you want)
# Example: { "username1": "password1", "username2": "password2" }
with open('pw.json') as pwf:
    pws = json.load(pwf)

# Setup Usernames and passwords for the TO/FROM GIS
FROM_UN = 'USER NAME OF THE FROM GIS'
FROM_PW = pws[FROM_UN]
TO_UN = 'USER NAME OF THE TO GIS'
TO_PW = pws[TO_UN]

# GIS Objects with the items to copy and GIS to put items into
try:
    FROM_GIS = gis.GIS(FROM_GIS_URL, FROM_UN, FROM_PW)
except RuntimeError as gis_err:
    print("An error occurred: ", gis_err)

try:
    TO_GIS = gis.GIS(TO_GIS_URL, TO_UN, TO_PW)
except RuntimeError as gis_err:
    print("An error occurred: ", gis_err)


def migrate_items(items):
    """
    Builds a list of items to migrate from a list of ids passed in
    :param items: list of item ids that will be migrated
    :return: list of items
    """
    migrate_list = []
    for item in items:
        try:
            migrate_list.append(gis.Item(FROM_GIS, item))
        except RuntimeError as err:
            print("An error occurred: ", err)
    return migrate_list


# Build items to migrate; list of item ids
items_to_migrate = migrate_items([
    # LIST YOUR APP/MAP/LAYER IDs HERE
])

# Content Manager to call clone_items method on
content_manager = gis.ContentManager(TO_GIS)

# Clone Items
try:
    content_manager.clone_items(items_to_migrate, folder=FOLDER_TO_PUT)
except Exception as error:
    print("Error cloning data: ", error)

Example will the "blanks" filled in for my own use (user names removed for privacy) and no pw.json file included (duh).

"""
Script to migrate GeosyntecMD items to Geosyntec AGOL.  Requires ArcGIS Python API 1.3+

Author: Dylan Walker
Date: 12/25/2017

"""
import json

from arcgis import gis

FOLDER_TO_PUT = 'some project folder'  # Folder on TO_GIS to put items into, optional
FROM_GIS_URL = 'https://geosyntecmd.maps.arcgis.com'  # URL of the GIS Portal/AGOL to copy FROM
TO_GIS_URL = 'https://geosyntec.maps.arcgis.com'  # URL of the GIS Portal/AGOL to copy TO

# Passwords JSON file - make this your own; username is the key, password is the value
# Example: { "username1": "password1", "username2": "password2" }
with open('pw.json') as pwf:
    pws = json.load(pwf)

# Setup Usernames and passwords for the TO/FROM GIS
FROM_UN = "some user name here"
FROM_PW = pws[FROM_UN]
TO_UN = "another user name here"
TO_PW = pws[TO_UN]

# GIS Objects with the items to copy and GIS to put items into
try:
    FROM_GIS = gis.GIS(FROM_GIS_URL, FROM_UN, FROM_PW)
except RuntimeError as gis_err:
    print("An error occurred: ", gis_err)

try:
    TO_GIS = gis.GIS(TO_GIS_URL, TO_UN, TO_PW)
except RuntimeError as gis_err:
    print("An error occurred: ", gis_err)


def migrate_items(items):
    """
    Builds a list of items to migrate from a list of ids passed in
    :param items: list of item ids that will be migrated
    :return: list of items
    """
    migrate_list = []
    for item in items:
        try:
            migrate_list.append(gis.Item(FROM_GIS, item))
        except RuntimeError as err:
            print("An error occurred: ", err)
    return migrate_list


# Build items to migrate; list of item ids
items_to_migrate = migrate_items([
    'f165836f45934d11a94e7550a9ca7c97',    
    '21dc60858a0a42fdb2048ddaeba228ac',
    '0278345852fe4857a15c28fcdecac0e9',
    'c621e3e3a1c94e1a9119e1f4da2ea244',
    '2e4924e5ab064d5194cef95af9a4d3cb',
    '3bcdbc9649c64128872a4fd959dfcb8e',
    '1b6d489affd546a792f0699f83326950'
])

# Content Manager to call clone_items method on
content_manager = gis.ContentManager(TO_GIS)

# Clone Items
try:
    content_manager.clone_items(items_to_migrate, folder=FOLDER_TO_PUT)
except Exception as error:
    print("Error cloning data: ", error)