Apply a definition query to a Layer Group

1226
2
Jump to solution
11-09-2017 08:46 AM
PeteCoventry3
Occasional Contributor

I'm trying to apply a definition query to a Layer Group (containing several feature classes) by using the following script where I would simply change the values for my 'SUBCODE' field

import arcpy

#Variables to form defintion query
field = '"SUBCODE"'
values = "'1152','1153'"
#concatenate query syntax
queryStr = str(field) + "=" + str(value)
#Specify the aprx project (CURRENT), dataframe (Layers)
p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps("Map_3D")[0]
#Apply defintion query to specified layer group 
for lyr in m.listLayers( "3D Layers")[0]:
    if lyr.supports("DEFINITIONQUERY"): 
    lyr.definitionQuery = queryStr
arcpy.RefreshActiveView()       
del aprx

This is the error I'm receiving when I run this in Arcpy:

Traceback (most recent call last):
  File "<string>", line 12, in <module>
IndexError: list index out of range

 There are some migration from arcpy.mapping to ArcGIS Pro I'm having trouble with - http://pro.arcgis.com/en/pro-app/arcpy/mapping/migratingfrom10xarcpymapping.htm

Any help on this script would be greatly appreciated!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

when you list layer and grab the first item (index 0) you have a single element and not a list. Remove the "[0]" at the end of line 12

View solution in original post

2 Replies
XanderBakker
Esri Esteemed Contributor

when you list layer and grab the first item (index 0) you have a single element and not a list. Remove the "[0]" at the end of line 12

PeteCoventry3
Occasional Contributor

That was it, thanks so much!!

0 Kudos