Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a large number of groups created, with each group having many layers. I have looked for a way to set a definition query for all the layers in each group but haven't found anything. Is it even possible, maybe with model builder or python? I have ArcMap 9.3 and 10.

share|improve this question
up vote 6 down vote accepted

This function should handle this.

import arpcy
def definitionByGroup(definition = "", groupname = "", mxd = "CURRENT"):
    #create layer list for current map document
    layerlist = arcpy.mapping.ListLayers(arcpy.mapping.MapDocument(mxd))
    for layer in layerlist:
        if layer.isFeatureLayer:
            groups = layer.longName.split('\\')
            if groupname == "" and layer.supports("DEFINITIONQUERY"):
                layer.definitionQuery = definition
            if len(groups) > 1 and groupname in groups and layer.supports("DEFINITIONQUERY"):
                layer.definitionQuery = definition

Copy paste into the arcpy window and it will be available.


Change definition to "OBJECTID = 1" for all feature layers in group "Test" in the current map

definitionByGroup("OBJECTID = 1", "Test")

Same change, but in the map stored at "C:\test.mxd"

definitionByGroup("OBJECTID = 1", "Test", "C:\text.mxd")

Clear definitions on all feature layers in group "Test"

definitionByGroup("", "Test")

Set definition on all feature layers in current map

definitionByGroup("OBJECTID = 1")

Clear all definitions in current map


Clear all definitions in map at "C:\text.mxd"

definitionByGroup("", "", "C:\text.mxd")

This function will also scan nested groups, matching on any layer of the group nesting.

If you want this to be an importable script into ArcMap, save this to a .py file in the pythonpath. e.g. save as and then import and use with:

import definitionByGroup
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.