<?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 Batch multiple definition query in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653719#M74742</link>
    <description>&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;def main():&lt;BR /&gt;# --- Tool parameters ---&lt;BR /&gt;gdb = arcpy.GetParameterAsText(0) # Input Geodatabase&lt;BR /&gt;field_name = arcpy.GetParameterAsText(1) # Field to use in query (e.g., project_number)&lt;BR /&gt;where_clause = arcpy.GetParameterAsText(2) # SQL clause (e.g., project_number = 'ABC' OR project_number = 'XYZ')&lt;BR /&gt;clear_all = arcpy.GetParameter(3) # Boolean checkbox: True = clear all definition queries&lt;/P&gt;&lt;P&gt;# Get current project&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject("CURRENT")&lt;/P&gt;&lt;P&gt;# Walk through all feature classes in the GDB&lt;BR /&gt;for dirpath, dirnames, fcs in arcpy.da.Walk(gdb, datatype="FeatureClass"):&lt;BR /&gt;for fc in fcs:&lt;BR /&gt;fc_path = os.path.join(dirpath, fc)&lt;/P&gt;&lt;P&gt;# If not clearing → check that the field exists before applying query&lt;BR /&gt;if not clear_all:&lt;BR /&gt;if not arcpy.ListFields(fc_path, field_name):&lt;BR /&gt;arcpy.AddWarning(f"{field_name} not found in {fc}, skipping")&lt;BR /&gt;continue&lt;/P&gt;&lt;P&gt;# Go through all maps and layers in the project&lt;BR /&gt;for m in aprx.listMaps():&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isFeatureLayer:&lt;BR /&gt;try:&lt;BR /&gt;# Match the layer's data source with this feature class path&lt;BR /&gt;if os.path.normcase(os.path.normpath(lyr.dataSource)) == os.path.normcase(os.path.normpath(fc_path)):&lt;BR /&gt;&lt;BR /&gt;# --- CLEAR ALL option ---&lt;BR /&gt;if clear_all:&lt;BR /&gt;lyr.definitionQuery = ""&lt;BR /&gt;arcpy.AddMessage(f"Cleared query on {lyr.name}")&lt;BR /&gt;&lt;BR /&gt;# --- APPLY QUERY option ---&lt;BR /&gt;else:&lt;BR /&gt;if where_clause:&lt;BR /&gt;lyr.definitionQuery = where_clause&lt;BR /&gt;arcpy.AddMessage(f"Applied query on {lyr.name}: {where_clause}")&lt;BR /&gt;else:&lt;BR /&gt;arcpy.AddWarning(f"No where clause provided for {lyr.name}, skipped")&lt;/P&gt;&lt;P&gt;except Exception as e:&lt;BR /&gt;arcpy.AddWarning(f"Could not update {lyr.name}: {e}")&lt;/P&gt;&lt;P&gt;# Save changes to project&lt;BR /&gt;aprx.save()&lt;/P&gt;&lt;P&gt;if __name__ == "__main__":&lt;BR /&gt;main()&lt;/P&gt;</description>
    <pubDate>Sun, 28 Sep 2025 17:16:27 GMT</pubDate>
    <dc:creator>RiznaAbdulgafoor</dc:creator>
    <dc:date>2025-09-28T17:16:27Z</dc:date>
    <item>
      <title>Batch multiple definition query</title>
      <link>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653719#M74742</link>
      <description>&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;def main():&lt;BR /&gt;# --- Tool parameters ---&lt;BR /&gt;gdb = arcpy.GetParameterAsText(0) # Input Geodatabase&lt;BR /&gt;field_name = arcpy.GetParameterAsText(1) # Field to use in query (e.g., project_number)&lt;BR /&gt;where_clause = arcpy.GetParameterAsText(2) # SQL clause (e.g., project_number = 'ABC' OR project_number = 'XYZ')&lt;BR /&gt;clear_all = arcpy.GetParameter(3) # Boolean checkbox: True = clear all definition queries&lt;/P&gt;&lt;P&gt;# Get current project&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject("CURRENT")&lt;/P&gt;&lt;P&gt;# Walk through all feature classes in the GDB&lt;BR /&gt;for dirpath, dirnames, fcs in arcpy.da.Walk(gdb, datatype="FeatureClass"):&lt;BR /&gt;for fc in fcs:&lt;BR /&gt;fc_path = os.path.join(dirpath, fc)&lt;/P&gt;&lt;P&gt;# If not clearing → check that the field exists before applying query&lt;BR /&gt;if not clear_all:&lt;BR /&gt;if not arcpy.ListFields(fc_path, field_name):&lt;BR /&gt;arcpy.AddWarning(f"{field_name} not found in {fc}, skipping")&lt;BR /&gt;continue&lt;/P&gt;&lt;P&gt;# Go through all maps and layers in the project&lt;BR /&gt;for m in aprx.listMaps():&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isFeatureLayer:&lt;BR /&gt;try:&lt;BR /&gt;# Match the layer's data source with this feature class path&lt;BR /&gt;if os.path.normcase(os.path.normpath(lyr.dataSource)) == os.path.normcase(os.path.normpath(fc_path)):&lt;BR /&gt;&lt;BR /&gt;# --- CLEAR ALL option ---&lt;BR /&gt;if clear_all:&lt;BR /&gt;lyr.definitionQuery = ""&lt;BR /&gt;arcpy.AddMessage(f"Cleared query on {lyr.name}")&lt;BR /&gt;&lt;BR /&gt;# --- APPLY QUERY option ---&lt;BR /&gt;else:&lt;BR /&gt;if where_clause:&lt;BR /&gt;lyr.definitionQuery = where_clause&lt;BR /&gt;arcpy.AddMessage(f"Applied query on {lyr.name}: {where_clause}")&lt;BR /&gt;else:&lt;BR /&gt;arcpy.AddWarning(f"No where clause provided for {lyr.name}, skipped")&lt;/P&gt;&lt;P&gt;except Exception as e:&lt;BR /&gt;arcpy.AddWarning(f"Could not update {lyr.name}: {e}")&lt;/P&gt;&lt;P&gt;# Save changes to project&lt;BR /&gt;aprx.save()&lt;/P&gt;&lt;P&gt;if __name__ == "__main__":&lt;BR /&gt;main()&lt;/P&gt;</description>
      <pubDate>Sun, 28 Sep 2025 17:16:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653719#M74742</guid>
      <dc:creator>RiznaAbdulgafoor</dc:creator>
      <dc:date>2025-09-28T17:16:27Z</dc:date>
    </item>
    <item>
      <title>Re: Batch multiple definition query</title>
      <link>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653774#M74743</link>
      <description>&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;def main():&lt;BR /&gt;# --- Tool parameters ---&lt;BR /&gt;gdb = arcpy.GetParameterAsText(0) # Input Geodatabase&lt;BR /&gt;field_name = arcpy.GetParameterAsText(1) # Field to use in query (e.g., project_number)&lt;BR /&gt;where_clause = arcpy.GetParameterAsText(2) # SQL clause (e.g., project_number = 'ABC' OR project_number = 'XYZ')&lt;BR /&gt;clear_all = arcpy.GetParameter(3) # Boolean checkbox: True = clear all definition queries&lt;/P&gt;&lt;P&gt;# Get current project&lt;BR /&gt;aprx = arcpy.mp.ArcGISProject("CURRENT")&lt;/P&gt;&lt;P&gt;# Walk through all feature classes in the GDB&lt;BR /&gt;for dirpath, dirnames, fcs in arcpy.da.Walk(gdb, datatype="FeatureClass"):&lt;BR /&gt;for fc in fcs:&lt;BR /&gt;fc_path = os.path.join(dirpath, fc)&lt;/P&gt;&lt;P&gt;# If not clearing → check that the field exists before applying query&lt;BR /&gt;if not clear_all:&lt;BR /&gt;if not arcpy.ListFields(fc_path, field_name):&lt;BR /&gt;arcpy.AddWarning(f"{field_name} not found in {fc}, skipping")&lt;BR /&gt;continue&lt;/P&gt;&lt;P&gt;# Go through all maps and layers in the project&lt;BR /&gt;for m in aprx.listMaps():&lt;BR /&gt;for lyr in m.listLayers():&lt;BR /&gt;if lyr.isFeatureLayer:&lt;BR /&gt;try:&lt;BR /&gt;# Match the layer's data source with this feature class path&lt;BR /&gt;if os.path.normcase(os.path.normpath(lyr.dataSource)) == os.path.normcase(os.path.normpath(fc_path)):&lt;BR /&gt;&lt;BR /&gt;# --- CLEAR ALL option ---&lt;BR /&gt;if clear_all:&lt;BR /&gt;lyr.definitionQuery = ""&lt;BR /&gt;arcpy.AddMessage(f"Cleared query on {lyr.name}")&lt;BR /&gt;&lt;BR /&gt;# --- APPLY QUERY option ---&lt;BR /&gt;else:&lt;BR /&gt;if where_clause:&lt;BR /&gt;lyr.definitionQuery = where_clause&lt;BR /&gt;arcpy.AddMessage(f"Applied query on {lyr.name}: {where_clause}")&lt;BR /&gt;else:&lt;BR /&gt;arcpy.AddWarning(f"No where clause provided for {lyr.name}, skipped")&lt;/P&gt;&lt;P&gt;except Exception as e:&lt;BR /&gt;arcpy.AddWarning(f"Could not update {lyr.name}: {e}")&lt;/P&gt;&lt;P&gt;# Save changes to project&lt;BR /&gt;aprx.save()&lt;/P&gt;&lt;P&gt;if __name__ == "__main__":&lt;BR /&gt;main()&lt;/P&gt;</description>
      <pubDate>Mon, 29 Sep 2025 09:05:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653774#M74743</guid>
      <dc:creator>RiznaAbdulgafoor</dc:creator>
      <dc:date>2025-09-29T09:05:30Z</dc:date>
    </item>
    <item>
      <title>Re: Batch multiple definition query</title>
      <link>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653786#M74744</link>
      <description>&lt;P&gt;your code is hard to read since it is unformatted, see&lt;/P&gt;&lt;P&gt;&lt;A href="https://community.esri.com/t5/python-blog/code-formatting-the-community-version/ba-p/1007633" target="_blank"&gt;Code formatting ... the Community Version - Esri Community&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 29 Sep 2025 09:22:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653786#M74744</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2025-09-29T09:22:55Z</dc:date>
    </item>
    <item>
      <title>Re: Batch multiple definition query</title>
      <link>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653858#M74745</link>
      <description>&lt;P&gt;You're getting really heavy with the nesting and it's hard to follow the plot. You also haven't really asked a question, just posted code and a title explaining what your code seems to be attempting to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Either way I went ahead and tried to make your code a bit more readable:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
import os
from pathlib import Path
from collections.abc import Iterator
from arcpy._mp import Layer, Map

def apply_query(lyr: Layer, query: str|None) -&amp;gt; None:
    if query is not None:
        lyr.updateDefinitionQueries([{'name': 'Query', 'sql': query, 'isActive': True}])
        return
    qs = lyr.listDefinitionQueries()
    for q in qs:
        q['isActive'] = False
    lyr.updateDefinitionQueries(qs)

def get_feature_layers(m: Map) -&amp;gt; Iterator[Layer]:
    yield from (l for l in m.listLayers() if l.isFeatureLayer)

def has_field(lay: Layer, field: str) -&amp;gt; bool:
    return [field in arcpy.da.SearchCursor(lay, '*').fields].pop()

def main():
    # --- Tool parameters ---
    gdb = arcpy.GetParameterAsText(0) # Input Geodatabase
    gdb_path = Path(gdb)
    field_name = arcpy.GetParameterAsText(1) # Field to use in query (e.g., project_number)
    where_clause = arcpy.GetParameterAsText(2) # SQL clause (e.g., project_number = 'ABC' OR project_number = 'XYZ')
    clear_all = arcpy.GetParameter(3) # Boolean checkbox: True = clear all definition queries
    
    aprx = arcpy.mp.ArcGISProject("CURRENT")
    
    # Iterate through tuples of map, layer using the get_feature_layers filter
    for map, layer in ((m, l) for m in aprx.listMaps() for l in get_feature_layers(m)):
        _ident = f'{map.name}-&amp;gt;{layer.longName}' # For logging
        
        # This will make sure only layers that are sourced to the GDB are processed
        if not Path(layer.dataSource).is_relative_to(gdb_path):
            continue
        
        # This will make sure that the field exists in the layer if you aren't clearing
        if not clear_all and not has_field(layer, field_name):
            arcpy.AddWarning(f"{field_name} not found in {_ident}, skipping") 
            continue
        
        # This will apply the query or clear the query
        try: apply_query(layer, None if clear_all else where_clause)
        except Exception as e:
            arcpy.AddWarning(f"Could not update {_ident}: {e}")
            continue # Don't print the success message
        
        arcpy.AddMessage(f"Applied query on {_ident}: {where_clause}")
        
if __name__ == "__main__":
    main()&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 29 Sep 2025 14:10:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/batch-multiple-definition-query/m-p/1653858#M74745</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2025-09-29T14:10:32Z</dc:date>
    </item>
  </channel>
</rss>

