PROCÉDURE
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.
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.
from arcgis.gis import GIS
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)
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)
num_items = 5 items = gis1.content.search(query="owner: {}".format(username_1), max_items=num_items, sort_field='id', sort_order='desc')
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')
itemid = '<item_ID>' #insert the item id items = gis1.content.search(itemid)
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)
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)
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)
Obtenir de l’aide auprès des experts ArcGIS
Télécharger l’application Esri Support