I am trying to find a specific field name in layers in a current working MXD, but I'm having problems getting it right. The overall goal is to loop through layers in an MXD and set a definition query based on a user input (string type). The script runs without any errors, but no definition query is set.
Could it be because the type returned by the arcpy.Describe().fields function is Unicode?
Any help or advice is greatly appreciated!
import arcpy
#User input as 'String' type
feederID = arcpy.GetParameterAsText(0)
#Variables
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Set definition query to user set parameters
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
fields = arcpy.Describe(lyr).fields
for f in fields:
if f.Name == "FEEDERID":
lyr.definitionQuery = """FEEDERID = """ + "'" + str(feederID) + "'"
arcpy.RefreshActiveView()
Solved! Go to Solution.
I'd try:
#Set definition query to user set parameters
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isFeatureLayer: #<---this bit will ensure you are trying to access the fields of only feature layers
fields = arcpy.Describe(lyr).fields
for f in fields:
if f.Name == "FEEDERID":
lyr.definitionQuery = """FEEDERID = """ + "'" + str(feederID) + "'"
What about saving the mxd with arcpy? One would think that the call to arcpy.RefreshActiveView() would do it, but...
You could also add the below just to make it a little safer...
if lyr.supports("DEFINITIONQUERY"):
This addition was added to the script but still no results.
did you throw in some prints to see what is returned .....and no... unicode is just fine.. it is the way of the future that began many years ago when 3.x was introduced
Is feederID a number? If so, does this work?
lyr.definitionQuery = "FEEDERID = " + str(feederID)
This works for me:
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
... df = arcpy.mapping.ListDataFrames(mxd)[0]
... feeder_field = "FEEDERID"
... value = "something"
... for lyr in arcpy.mapping.ListLayers(mxd, "", df):
... fields = arcpy.Describe(lyr).fields
... for f in fields:
... if f.Name == feeder_field:
... delim = arcpy.AddFieldDelimiters(lyr,feeder_field)
... lyr.definitionQuery = delim + "='" + value + "'"
Darren,
Thanks for the help.
I am currently running this Pythron script (v. 2.7.8) as a Script tool in a toolbox in ArcMap (v. 10.3.1).
When I test the code in the Python Window in ArcMap, I am able to list the fields in the (for f in fields:) loop. But, at the end of the prints, I am getting this error.
Any ideas as to why?
Are you looping over fields in a layer or multiple layers? If the latter, are any of them group layers, map service layers, etc.... The error you describe will get generated if you try to return the fields of a layer that doesn't have fields.
Josh,
Thank you for the time and the response. The layers are multiple map layers in a feature dataset residing in an enterprise geodatabase.
I'd try:
#Set definition query to user set parameters
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isFeatureLayer: #<---this bit will ensure you are trying to access the fields of only feature layers
fields = arcpy.Describe(lyr).fields
for f in fields:
if f.Name == "FEEDERID":
lyr.definitionQuery = """FEEDERID = """ + "'" + str(feederID) + "'"