<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Clone items, clone a map and only x layers in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/clone-items-clone-a-map-and-only-x-layers/m-p/1613241#M11366</link>
    <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i've tried this but it copies all 4 layers, i even tried the item_mapping and used the same id as both&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;any ideas?&lt;/P&gt;</description>
    <pubDate>Fri, 09 May 2025 14:29:32 GMT</pubDate>
    <dc:creator>StuartMoore</dc:creator>
    <dc:date>2025-05-09T14:29:32Z</dc:date>
    <item>
      <title>Clone items, clone a map and only x layers</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/clone-items-clone-a-map-and-only-x-layers/m-p/1613241#M11366</link>
      <description>&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i've tried this but it copies all 4 layers, i even tried the item_mapping and used the same id as both&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;any ideas?&lt;/P&gt;</description>
      <pubDate>Fri, 09 May 2025 14:29:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/clone-items-clone-a-map-and-only-x-layers/m-p/1613241#M11366</guid>
      <dc:creator>StuartMoore</dc:creator>
      <dc:date>2025-05-09T14:29:32Z</dc:date>
    </item>
    <item>
      <title>Re: Clone items, clone a map and only x layers</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/clone-items-clone-a-map-and-only-x-layers/m-p/1613721#M11367</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/159798"&gt;@StuartMoore&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;- create a new EXB&lt;BR /&gt;- create a new WebMap&lt;BR /&gt;- remove layers from the WebMap&lt;BR /&gt;- update the EXB to match the old and with new WebMap&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All handled in the code below (API version 2.4.0).&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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 &amp;amp; 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 &amp;amp; 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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let us know if this works for you.&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Glen&lt;/P&gt;</description>
      <pubDate>Mon, 12 May 2025 17:15:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/clone-items-clone-a-map-and-only-x-layers/m-p/1613721#M11367</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-05-12T17:15:21Z</dc:date>
    </item>
  </channel>
</rss>

