i have an experience builder app which has a map and the map has 4 layers in it, i want to clone the app and the map and only 1 of the 4 layers and then to remap the cloned feature service into the cloned map.
i've tried this but it copies all 4 layers, i even tried the item_mapping and used the same id as both
from pathlib import Path
import sys
import pandas as pd
from arcgis.gis import GIS, Item
from arcgis.env import active_gis
from arcgis.features import FeatureLayerCollection
from arcgis.mapping import WebMap
source = GIS("home")
print(source)
target = GIS("home")
print(target)
group = source.groups.search("id: 8d..fd")[0]
for item in group.content():
print(item['title'])
print(item['id'])
xid = item['id']
items = source.content.search(query="id:{0}".format(xid))
#items
for item in items:
try:
print("Cloning " + item.title)
copy_list = []
copy_list.append(item)
print (copy_list)
if (item.title == "In_Map"):
item_mapping = {"db..d77" : "db..d77"}
target.content.clone_items(copy_list, copy_data=True, search_existing_items=True, item_mapping = item_mapping, folder="In_Map_Test_version")
else:
target.content.clone_items(copy_list, copy_data=True, search_existing_items=True,folder="In_Map_Test_version")
print("Successfully cloned " + item.title)
except Exception as e:
print(e)
any ideas?
Hi @StuartMoore,
Clone was really created to clone from one AGOL to another (or Portal). While it kinda works for some items to clone in the same AGOL, it also kinda doesn't and can cause issues. Anyway, I took the challenge of what you are trying to achieve and here's a possible solution.
- create a new EXB
- create a new WebMap
- remove layers from the WebMap
- update the EXB to match the old and with new WebMap
All handled in the code below (API version 2.4.0).
from arcgis import GIS
from arcgis.apps.expbuilder.expbuilder import WebExperience
from arcgis.map import Map
import ast
## access ArcGIS Online
agol = GIS("home")
################################################################################
## USER INPUT ##################################################################
exb_id = "EXB_ITEM_ID"
new_exb_name = "CLONED!"
wm_id = "WEBMAP_ITEM_ID"
layers2keep = ["LAYER_NAME"]
new_wm_name = "NEW_WEBMAP"
################################################################################
## GET ITEM OBJECTS ############################################################
## get the Item object for the EXB you want to "clone"
exb_item = agol.content.get(exb_id)
## get the WebMap as an Item object
wm_item = agol.content.get(wm_id)
################################################################################
## CREATE NEW WEBMAP ###########################################################
## create Map object
webmap = Map(wm_item)
## save a new webmap
new_wm_item = webmap.save(
item_properties = {
"title" : new_wm_name,
"snippet" : wm_item.snippet,
"tags" : wm_item.tags
}
)
################################################################################
## GET JSON DEFINITION FOR NEW WEBMAP & MANIPULATE #############################
## get the JSON definition for the webmap as a dictionary
webmap_data = new_wm_item.get_data()
## empty list to hoild the layer we want
new_ol = []
## iterate through map layer and only add the ones of interest
for lyr in webmap_data["operationalLayers"]:
if lyr["title"] in layers2keep:
new_ol.append(lyr)
## set the new operationalLayers property
webmap_data["operationalLayers"] = new_ol
## update the new WebMap to only contain the layers of interest
new_wm_item.update(
item_properties = {"text" : webmap_data}
)
################################################################################
## GET JSON DEFINITION FOR EXB & MANIPULATE ####################################
## get the JSON definition for the EXB as a string
exb_data = str(exb_item.get_data())
## replace the instance of teh old WebMap ID with new
exb_data = exb_data.replace(wm_id, new_wm_item.id)
## replace the name of the old WebMap with new
exb_data = exb_data.replace(wm_item.title, new_wm_item.title)
## convert string back to a dictionary
exb_data = ast.literal_eval(exb_data)
################################################################################
## CREATE NEW EXB ##############################################################
## create a new EXB
new_exb = WebExperience(name=new_exb_name)
## publish the new EXB (optional)
new_exb.save(publish=True)
## get the Item object for the new EXB
new_exb_item = new_exb.item
## update the JSON definition for the new EXB
new_exb_item.update({"text" : exb_data})
################################################################################
print("\nSCRIPT COMPLETE")
Please let us know if this works for you.
All the best,
Glen