jakhowe

Search all Layers for Field Name and Perform Definition Query

Discussion created by jakhowe on May 3, 2012
Latest reply on May 3, 2012 by mzcoyle
I apologize in advance, I'm a newbie with Python programming.

My question is, I have a mxd with many layers, I want a script that will search all of the layers with a field name of "SUB_CODE" and if that layer has that field, I want the script to perform a definition query of "SUB_CODE = 143."  Below I have the code as it is right now but doesn't perform the field name search.  Your help is appreciated.

Thanks,

Jakob

import arcpy
queryStr = "SUB_CODE=143"
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
  if lyr.supports("DEFINITIONQUERY"): 
  lyr.definitionQuery = queryStr
        
del mxd

Outcomes