I am using ArcGIS Pro 3.x.
I'm trying to make a script to automate changing the definition query for multiple layers in a group layer in Pro. The definition query is essentially just using a project ID number ("TDS_WBS_CODE") to filter the data to a specific project.
The goal is to change the active definition query for multiple layers in a group layer and not have a list of unused queries.
My current code seems to fail at the "# Change the Definition query of target layers" comment.
import arcpy
# access current pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
# access current map frame
m = aprx.activeMap
# access layers in TOC
lyrList = m.listLayers()
# list of target layer names
ADP_GIS_EDIT = ["ADP_GIS_EDIT\FiberSpliceEnclosure", "ADP_GIS_EDIT\AccessPoint",
"ADP_GIS_EDIT\FiberSplitter", "ADP_GIS_EDIT\Pole",
"ADP_GIS_EDIT\FiberCable", "ADP_GIS_EDIT\Conduit", "ADP_GIS_EDIT\ServingArea"]
# loop that grabs "PortConnection" table
for m in aprx.listMaps():
for tbl in m.listTables():
if tbl.longName == "ADP_GIS_EDIT\PortConnection":
print(tbl.longName)
# loop that grabs target features within "ADP_GIS_EDIT" group layer
for l in lyrList:
if l.isGroupLayer:
lyrs = l.listLayers()
for lyr in lyrs:
if lyr.longName in ADP_GIS_EDIT:
# Change the Definition query of target layers
cim_lyr = lyr.getDefinition("V2")
if cim_lyr.featureTable.definitionFilterChoices:
cim_lyr.featureTable.definitionFilterChoices[0].definitionExpression = "TDS_WBS_CODE = 'TC-211606035'"
cim_lyr.featureTable.definitionExpression = None
else:
cim_lyr.featureTable.definitionExpression = None
lyr.setDefinition(cim_lyr)