AnsweredAssumed Answered

Update WebMap dictionary for multiple feature layers

Question asked by LANDVEST on Nov 4, 2019
Latest reply on Jan 30, 2020 by antti.kajanus_CGISuomi

There are several threads on here that have gotten me this far, but I've still not found any solid documentation about what needs to be supplied to the JSON dictionary in order to properly update it. Note: I can get this to work when there is only one layer in the feature layer/feature collection. I'm just stuck.

 

Here is my code so far.

"""
#Import Modules
import copy,json
from arcgis.gis import GIS
from arcgis.mapping import WebMap

#Log into ArcGIS Enterprise
gis = GIS("https://awesomeportalname.com/portal", "DougMcGrave", "a1ntL!FeGr@nd!")
print("Logged in as: " + gis.properties.user.username)


#Retrieve NortheastFieldData web map
webMapSearch = gis.content.search("title: NortheastFieldData2",item_type = "Web Map")
sourceWebMap= webMapSearch[0]
#itemID = webMapTest.id
#print(itemID)
#Read the web map as a WebMap object
webMapObject = WebMap(sourceWebMap)

#Create a copy of the web map with a new title
webmap_item_properties = {'title': 'TestCopyThis', 'snippet':'testcopydis', 'tags':'Test'}
webMapObject.save(webmap_item_properties)

#Retrieve new web map
newWebMapSearch = gis.content.search("title: TestCopyThis",item_type = "Web Map")
newWebMap = newWebMapSearch[0]


for layer in newWebMap.layers:
    print(layer.title)

#Extract JSON from webmap
pubMapJson = newWebMap.get_data(try_json=True)
jsonCopy = copy.deepcopy(pubMapJson)

#Layers in web map which need filter applied
layersList = ['NortheastCruisePoints','NortheastClientsCollector - NortheastPoints','NortheastClientsCollector - NortheastPoints','NortheastClientsCollector - NortheastLines','NortheastClientsCollector - NortheastStands','NortheastClientsCollector - Harvest Operations Status']

#Filter to be applied
lyrFilter = "LVI_CODE = '{0}'".format("7532")
   
#Extract JSON from webmap and create a copy
pubMapJson = newWebMap.get_data(try_json=True)
jsonCopy = copy.deepcopy(pubMapJson)

#Loop through each layer
for lyr in layersList:
        if 'layerDefinition' in lyr:
            print(lyr)
            jsonCopy['operationalLayers'][0]['layerDefinition']['definitionExpression']=lyrFilter
           
#Update JSON
newWebMap.update(item_properties={'text':json.dumps(jsonCopy)})

No errors are given and the JSON update returns 'True'

Outcomes