AnsweredAssumed Answered

Python definition query - script created in ArcMap to be used in Pro

Question asked by pcoventry_bouldercounty on Nov 3, 2017
Latest reply on Nov 3, 2017 by pcoventry_bouldercounty

My goal is to create a script that applies a definition query to multiple layers in ArcGIS Pro. I was able to successfully create a script (not the most elegant) in ArcMap and not knowing that the GUI is different in Pro and causing errors to my script.

 

I'm new to Python and doing my best to recreate this script in ArcGIS Pro, but having difficulty especially using the listLayer.    

 

Any help would be greatly appreciated!

 

Here's my script created in ArcMap:

     

mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_1ST_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_2ND_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_3RD_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_4TH_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"CONDO_5TH_LEVEL*")
#replace subcode here
query = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery

 

Here's my attempt to convert the script to version 3 to use in ArcGIS Pro:

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_1ST_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_2ND_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_3RD_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_4TH_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)
aprx = arcpy.mp.ArcGISProject("CURRENT")
lyr = m.listLayers(aprx,"CONDO_5TH_LEVEL*")[0]
#replace subcode here
query = '"SUBCODE" =\'1172\''
m.listLayers[0].definitionQuery=query
print (layers [0].definitionQuery)

Outcomes