<?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 Re: Updating Web Map Layer Definition Expression Filters via Python 2.4 in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584992#M11157</link>
    <description>&lt;P&gt;Thank you so much, this works perfect! I voted for your idea, hopefully it gets brought back!&lt;/P&gt;</description>
    <pubDate>Thu, 13 Feb 2025 16:20:03 GMT</pubDate>
    <dc:creator>BBarbs</dc:creator>
    <dc:date>2025-02-13T16:20:03Z</dc:date>
    <item>
      <title>Updating Web Map Layer Definition Expression Filters via Python 2.4</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584483#M11149</link>
      <description>&lt;P&gt;Hello Community,&lt;/P&gt;&lt;P&gt;I recently upgraded ArcGIS Pro to 3.4.2, which included Python 2.4.0 and the new Map() module. I am working on updating one of my scripts and have run into a road block I was hoping someone could help me with.&lt;/P&gt;&lt;P&gt;My map has several grouped layers whose definition expressions are updated by python script. My usual method of defining each layer by title doesn't work anymore with the new mapping module. Does anyone know how I can point to my layers in Python 2.4's new module?&lt;/P&gt;&lt;P&gt;Here is a sample of my previous method:&lt;/P&gt;&lt;DIV&gt;&lt;P&gt;&lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;arcgis&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from&lt;/SPAN&gt; &lt;SPAN&gt;arcgis&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;map&lt;/SPAN&gt; &lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;Map&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from&lt;/SPAN&gt; &lt;SPAN&gt;arcgis&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;gis&lt;/SPAN&gt; &lt;SPAN&gt;import&lt;/SPAN&gt; &lt;SPAN&gt;GIS&lt;BR /&gt;&lt;/SPAN&gt;&lt;SPAN&gt;gis&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;GIS&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"home"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;EM&gt;# New method to grab web map in Python 2.4&lt;/EM&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;wm_item&lt;/SPAN&gt; &lt;SPAN&gt;=&lt;/SPAN&gt; &lt;SPAN&gt;gis&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;content&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;get&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"&amp;lt;item id&amp;gt;"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;webmap = Map(item=wm_item)&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;# report update date&lt;/EM&gt;&amp;nbsp;&lt;BR /&gt;reportDTSQL = '02012025'&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;# (Old, not-working method of updating layer expression-- NEEDS UPDATING)&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;# get layers of interest&lt;/EM&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;EM&gt;enrolledLyr =&lt;/EM&gt; webmap.get_layer(title="Locations&lt;EM&gt; Enrolled Only")&lt;/EM&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;EM&gt;# apply new filter expression to each layer's json&lt;/EM&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;enrolledLyr["layerDefinition"]["definitionExpression"]&lt;EM&gt; = f"(category = 'Active') AND ({reportDTSQL})"&lt;/EM&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;EM&gt;# Update web map object with new json&lt;/EM&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;webmap.update_layer(dict(EnrolledLyr))&lt;/DIV&gt;&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Thank you for any help!&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 12 Feb 2025 16:05:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584483#M11149</guid>
      <dc:creator>BBarbs</dc:creator>
      <dc:date>2025-02-12T16:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Web Map Layer Definition Expression Filters via Python 2.4</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584834#M11154</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/719798"&gt;@BBarbs&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I've abandoned using some of the new classes in version 2.4.0 and going direct to the WebMap definition as shown below, code commented for workflow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS

## access ArcGIS Online
agol = GIS("home")

## get teh WebMap as an Item object
wm_item = agol.content.get("WM_ITEM_ID")

## get the WebMap JSON definition
wm_item_data = wm_item.get_data()

## What layer do you want to update?
lyr_name = "LAYER_NAME"

## What filter do you want to apply?
filter_exp = "FIELD = 'VALUE'"

## for each layer in the WebMap
for lyr in wm_item_data["operationalLayers"]:
    ## if it is a Group Layer
    if lyr["layerType"] == "GroupLayer":
        ## for each layer in teh group layer
        for grp_lyr in lyr["layers"]:
            ## if the layer title is the one we want
            if grp_lyr["title"] == lyr_name:
                ## apply the filter
                grp_lyr["layerDefinition"]["definitionExpression"] = filter_exp

    ## if the layer of interest is not a group layer and matched the layer name
    elif lyr["title"] == lyr_name:
        ## apply the fiter
        lyr["layerDefinition"]["definitionExpression"] = filter_exp

## create the update dictionary
update_properties = {"text" : wm_item_data}

## apply the update to the WebMap object (updating JSON definition)
wm_item.update(
    item_properties = update_properties
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have asked for the get_layer() to be reinstated, you can vote for it &lt;A href="https://community.esri.com/t5/arcgis-api-for-python-ideas/get-web-map-layer-by-title-removed-in-2-4-0-bring/idc-p/1561224#M127" target="_blank" rel="noopener"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;All the best,&lt;/P&gt;&lt;P&gt;Glen&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 11:40:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584834#M11154</guid>
      <dc:creator>Clubdebambos</dc:creator>
      <dc:date>2025-02-13T11:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: Updating Web Map Layer Definition Expression Filters via Python 2.4</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584992#M11157</link>
      <description>&lt;P&gt;Thank you so much, this works perfect! I voted for your idea, hopefully it gets brought back!&lt;/P&gt;</description>
      <pubDate>Thu, 13 Feb 2025 16:20:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/updating-web-map-layer-definition-expression/m-p/1584992#M11157</guid>
      <dc:creator>BBarbs</dc:creator>
      <dc:date>2025-02-13T16:20:03Z</dc:date>
    </item>
  </channel>
</rss>

