Find Specific Field Name in MXD Layers

1568
14
Jump to solution
09-29-2016 11:39 AM
MitchHolley1
MVP Regular Contributor

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()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MicahBabinski
Regular Contributor

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) + "'"

View solution in original post

14 Replies
forestknutsen1
MVP Regular Contributor

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"):
MitchHolley1
MVP Regular Contributor

This addition was added to the script but still no results. 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

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

JoshuaBixby
MVP Esteemed Contributor

Is feederID a number?  If so, does this work?

lyr.definitionQuery = "FEEDERID = " + str(feederID)
DarrenWiens2
MVP Honored Contributor

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 + "'"
MitchHolley1
MVP Regular Contributor

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?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

MitchHolley1
MVP Regular Contributor

Josh, 

Thank you for the time and the response.  The layers are multiple map layers in a feature dataset residing in an enterprise geodatabase. 

0 Kudos
MicahBabinski
Regular Contributor

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) + "'"