I am in the process of cloning a number of dashboards for several organizations. I have created new web maps that need to be included within the cloned dashboards. In other words, when I clone, I'm swapping out the web maps that were originally in the dashboards and replacing them with new web maps.
I'm able to clone the dashboard and replace the old maps with the new maps; however, all of the other widgets are broken when I run the clone process (i.e., I'm getting a Data Source Error and the widgets are not able to access the data):
To figure out the best way to run my clone operation, I watched the following video:
https://www.youtube.com/watch?v=PhEM-k34bbY
My code to try to get this working is here:
# import libraries
from arcgis.gis import GIS
# connect to GIS org
gis = GIS(url, username, password)
# get the dashboard to be cloned by item ID
dash = gis.content.get("6c698f1b477645c381ffda09b9dac0b8")
dash_name = dash.title
print(dash)
# get existing map widget item ID
dash_map = [w for w in dash.get_data()['widgets'] if w['type'] == 'mapWidget'][0]
# create Python dictionary object to hold the web map items ids for the old WM and the new WM
# Key - old web map item id
# Value - new web map item id (new web map item ID is hard coded for now)
wm_mapping = {dash_map['itemId']: '351fa7da899c491ba061692dc785259f'}
# clone the dashboard while substituting the new web map (wm_mapping VALUE) in for the old web map (wm_mapping KEY)
cloned_dash_item = gis.content.clone_items(items=[dash], folder="zz_ClonedAppsWithUpdatedWebMaps", item_mapping=wm_mapping)
print(f'finished cloning {dash_name} dashboard')
Is there something I'm missing? Do I need to include the other widgets in my item mapping dictionary (line 18)? I made sure that the name of the layers that "feed" the various widgets didn't change between the old and the new web maps in case that would cause the widgets to break.
Any help is appreciated.
@pmccord did you ever manage to get this to work? I'm having the same issue with widgets not updating, as well as sometimes the map not actually updating.
Hi @abrown8 ,
You may need to update the widgets with data from the old maps. I was able to get this to work to some extent, but if I remember correctly, I still had to manually update a few things.
Please give the below a shot. Simply add the below code to the end of the above script (after line 22 above). You'll also need to import the json library
import json
at the beginning of your script.
Here's the code to add after line 22:
cloned_dash_data = cloned_dash_item[0].get_data()
# swap old layer source with new layer source
for widget in cloned_dash_data["widgets"]:
if "datasets" in widget:
old_lyr_src=widget["datasets"][0]["dataSource"]["layerId"]
new_lyr_src=lyr_mapping.get(old_lyr_src)
widget["datasets"][0]["dataSource"]["layerId"] = new_lyr_src
# udpate item
cloned_dash_item.update(item_properties={"text": json.dumps(cloned_dash_data)})
print(f"finished cloning {dash_name} dashboard")
Hi @pmccord @abrown8 - do you have an updated version of this script? I'm trying to do the same script above, the web map gets added into the cloned dashboard fine but I'm still not getting my widget in the cloned dashboard to update.
In the script this is the return I'm getting:
Traceback (most recent call last):
File "******", line 24, in <module>
cloned_dash_data = cloned_dash_item.get_data()
^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'get_data'
The cloned_dash_item object is a list, so you'll need to change to:
cloned_dash_data = cloned_dash_item[0].get_data()
I've revised my code above.