I have a project with several layers in the TOC that point to feature classes or shapefiles. Each of them is using labels and everything is ok.

If I replace the source of these layers with some other feature classes/shapefiles with different attribute fields, sometimes it happens that the atribute field for labeling is in the new source data missing. That means that in the layer properties the Label features in this layer checkbox is ticked but the window with the attribute field is blank. Then when refreshing the layout view, the ArcMap sometimes (I don´t know why only sometimes but still) throws an error window like this:

The error window that sometimes pops up

Since I can´t rely on ArcMap because the error window sometimes pops up and sometimes not, is there a way to use arcpy for looping through all layers in the TOC and find those with broken labels (=missing attribute field for labeling)? I can´t find anything about this possibility and no one has asked similar question.


2 Answers 2


Here's a little snippet of code that might get you started with looping through using arcpy. This is very basic and not tested but could be easily modified to suit your needs, I think.

layers = arcpy.mapping.ListLayers(yourMXD)
labelField = 'SomeFieldName'
for layer in layers:
    fields = [field.name for field in arcpy.ListFields(layer)]
    if labelField not in fields:
        print layer

There's also arcpy.mapping.ListBrokenDataSources() which you could add as well depending on your specific needs.

  • Thanks, I know how to loop through. The problem I´m looking the answer for is that I don´t know the attribute field for labeling because the window in the Label tab is blank - how do I find out which attribute field was set in the feature class that has been replaced?
    – jonlew
    Commented Aug 29, 2017 at 6:43

You're working with layer (*lyr) file so you have an access to it's properties: lyr.labelClasses (arcpy.mapping.Layer).

.labelClasses returns a list of label definitions. Get the first: layer.labelClasses[0], this will be an instance of LabelClass.

LabelClass has only a few properties. The one you need is labelClass.expression (arcpy.mapping.LabelClass). It will give you a single expression on VBA that ArcMaps uses to create labels. In case of single field it would be just '[SingleField]'. So that's it, you only need to parse the real name of the field and check if it exists in attribute table with arcpy.ListFields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.