Copy operational layers object from one web map to another

1667
3
Jump to solution
05-29-2018 10:52 AM
Jay_Gregory
Occasional Contributor III

I would like to use ArcGIS Python API (v. 1.4.1) to copy the operational layers from one map to another in the same 10.6 Portal (for a development map / production map environment where I make changes to the development maps' symbology, layers, popups, etc., and then just copy the entire operational layers object to the production map), but I can't get it to work (even though it worked on a previous version of the API / previous version of Portal).  

Here is my old code, which worked in a previous version.

In this version, the code successfully runs, but the destination map isn't changed.

from arcgis.gis import GIS
from arcgis.gis import Item
import arcgis.mapping

portal = GIS("https://portalurl/webcontext", "username","password", verify_cert=False)
srcMap = Item(portal, "5959197ba01748ccb51cf097597b4cf4")
destMap= Item(portal,"e326e343d5aa45668a448e54acc4d06d")
opsLayers = srcMap.get_data()['operationalLayers']
mapDestObj = arcgis.mapping.WebMap(destMap)
mapDestObj['operationalLayers'] = opsLayers
mapDestObj.update()‍‍‍‍‍‍‍‍‍‍‍

I've also followed instructions located at Using and updating GIS content | ArcGIS for Developers , but that doesn't seem to work either.  

mapDestObj has a layers attribute, but mapDestObj.layers = opsLayers throws an AttributeError (can't set attribute)

I've also tried the following: which successfully updates the mapDestObj.layers attribute, but doesn't save the map when I run the update command.  

for index, layer in enumerate(mapDestObj.layers):
    mapDestObj.layers[index] = opsLayers[index]
webMapObj.update({})

Does anyone have a working prototype for this workflow?

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Jay Gregory, in your first snippet, you may have to change the last but one line to the following

# your current line
# mapDestObj['operationalLayers'] = opsLayers‍
mapDestObj.definition['operationalLayers'] = opsLayers‍
mapDestObj.update()‍‍

and that should work. Can you give this a try and let me know?

View solution in original post

3 Replies
by Anonymous User
Not applicable

Jay Gregory, in your first snippet, you may have to change the last but one line to the following

# your current line
# mapDestObj['operationalLayers'] = opsLayers‍
mapDestObj.definition['operationalLayers'] = opsLayers‍
mapDestObj.update()‍‍

and that should work. Can you give this a try and let me know?

Jay_Gregory
Occasional Contributor III

Thanks - that worked!  It seems like the definition attribute isn't listed in the documentation.  

Also, I was able to get the operation to work by doing the following:

srcData = srcMap.get_data()
destData = destMap.get_data()
destData['operationalLayers'] = srcData['operationalLayers']
destMap.update(data=str(destData))
0 Kudos
by Anonymous User
Not applicable

You are welcome. For future reference, the correct way to do your workaround is to pass a dictionary with key= "text" and value = your web map dict. So the last line should look like this

import json
destMap.update(data={'text':json.dumps(destData)})
0 Kudos