Title: Refreshing display for newly added labels with ArcObjects from Python?
Tags: python;arcgis-10.1;arcobjects;labeling
Question: I've added a shapefile to an MXD and Edited the symbolization of it with ArcObjects.  Now I'm trying to edit the labeling with ArcObjects.  I've successfully been able to edit the default class in the IAnnotateLayerPropertiesCollection2 object.  I've also successfully added two more label classes to that collection object.

The default class that I edited is displaying correctly, but the extra two Label Classes aren't displaying after the script is run.  When I manually open the properties of the layer and open the properties for each label class that I added and choose OK or Apply, then they show up.  So I know that they are being added properly, but how do I get them to display through the script?

I've tried MxDoc.UpdateContets(), MxDoc.ActiveView.Refresh(), arcpy.RefreshActiveView() and none of them work for this.

I'm using Python to handle the ArcObjects, but if you know how to do it in VB or C#, I can interpolate it.  

```def ApplyLabels_arcobjects(layersource, pFLayer, pStyleGallery, carto):
    # If there are no labels in the custom symbology object
    if not layersource.symbology.labels:
        return

    # Get Annotation Properties Collection
    geofeaturelayer = Snippets.CType(pFLayer, carto.IGeoFeatureLayer)
    pAnnoPropColl = geofeaturelayer.AnnotationProperties
    pAnnoPropColl2 = Snippets.CType(pAnnoPropColl, carto.IAnnotateLayerPropertiesCollection2)

    # Loop through each label in the custom symbology object
    i = 0
    for labelsource in layersource.symbology.labels:
        print("This is label number {0}".format(i+1)) # TODO Remove, for debugging

        # Get the premade Label Style from the style file
        pStyleGallery.LoadStyle(labelsource.style_filename, labelsource.label_classname)
        gallerystyle = getLabelSymbol(labelsource, pStyleGallery, carto)
        pLabelStyle = Snippets.CType(gallerystyle, carto.ILabelStyle2)

        # If I can get a Annotation Property that is already there, great:
        if i <= pAnnoPropColl2.Count - 1:
            print("Editing the one already there")  # TODO Remove, for debugging
            pAnnoLayerProp = pAnnoPropColl2.QueryItem(i)[0]

            # Class Name
            pAnnoLayerProp.Class = labelsource.name

            # Sql string for Where expression
            pAnnoLayerProp.WhereClause = labelsource.sql_string

            # Set Sympol and Placement Properties
            pLabelEngineProp = Snippets.CType(pAnnoLayerProp, carto.ILabelEngineLayerProperties2)
            pLabelEngineProp.Symbol = pLabelStyle.Symbol
            pLabelEngineProp.OverposterLayerProperties = pLabelStyle.OverposterLayerProperties

        # If I have to add an Annotation Property to the Collection, then:
        else:
            print("Adding new Label Engine")# TODO Remove, for debugging
            # New Label Engine
            pLabelEngineProp = Snippets.NewObj(carto.MaplexLabelEngineLayerProperties, carto.ILabelEngineLayerProperties2)

            # Apply Symbol
            pLabelEngineProp.Symbol = pLabelStyle.Symbol

            # Apply Placement Properties
            pLabelEngineProp.OverposterLayerProperties = pLabelStyle.OverposterLayerProperties

            # Cast
            pAnnoLayerProp = Snippets.CType(pLabelEngineProp, carto.IAnnotateLayerProperties)
            # Class Name
            pAnnoLayerProp.Class = labelsource.name
            # Sql string for Where expression
            pAnnoLayerProp.WhereClause = labelsource.sql_string
            # Display Annotation
            pAnnoLayerProp.DisplayAnnotation = True
            # Add it to the collection
            pAnnoPropColl2.Add(pAnnoLayerProp)

        # Turn on Annotation for the Map Layer
        geofeaturelayer.DisplayAnnotation = True

        i += 1
```



Here is another answer: I've not done much with labelling with ArcObjects so this may not be relevant or you have tried it already? Have you tried setting IAnnotateLayerProperties.DisplayAnnotation Property?

Comment for this answer: Yes I have.  I'll add my code too.
Comment for this answer: I'm not familiar with Python but what I've done with ArcObject_C# is something like this:Write a class which implements IExtension interface,in this class use a varible for IActiveView and assign it properly.Then cast it to IActiveViewEvents_Event interface.Now you can overload the AfterDraw event or ItemAddes,ItemDeleted.In that overloaded event you can write your code to refresh map view.
Comment for this answer: You should make this an answer. I can't test it anymore but it sounds like it might work.

Here is another answer: Try this ELSE statement:

```else:
    print("Adding new Label Engine")# TODO Remove, for debugging
    # New Label Engine
    pLabelEngineProp = Snippets.NewObj(carto.MaplexLabelEngineLayerProperties, carto.ILabelEngineLayerProperties2)

    # Apply Symbol
    pLabelEngineProp.Symbol = pLabelStyle.Symbol

    # Apply Placement Properties
    pLabelEngineProp.OverposterLayerProperties = pLabelStyle.OverposterLayerProperties

    # Cast
    pAnnoLayerProp = Snippets.CType(pLabelEngineProp, carto.IAnnotateLayerProperties)
    # Class Name
    pAnnoLayerProp.Class = labelsource.name
    # Sql string for Where expression
    pAnnoLayerProp.WhereClause = labelsource.sql_string

    ((ILabelEngineLayerProperties2)pAnnoLayerProp).Expression = labelExpression;
    ((ILabelEngineLayerProperties2)pAnnoLayerProp).Symbol = pLabelStyle.Symbol;
    ((ILabelEngineLayerProperties2)pAnnoLayerProp).OverposterLayerProperties = pLabelStyle.OverposterLayerProperties;

    # Display Annotation
    pAnnoLayerProp.DisplayAnnotation = True

    # Clear existing labelling classes
    pAnnoPropColl2.Clear()

    # Add it to the collection
    pAnnoPropColl2.Add(pAnnoLayerProp)

    # Turn on Annotation for the Map Layer
    geofeaturelayer.DisplayAnnotation = True
```


Here is another answer: AnnotationProperties is a get/put property meaning a reference is not handed out. You need to set the collection back into the FeatureLayer after modifying it.


{'source': 'StackExchangeDataset', 'subset': 'gis.stackexchange.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:10/20/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><arcgis-10.1><arcobjects><labeling>', 'Title': 'Refreshing display for newly added labels with ArcObjects from Python?', 'ViewCount': 740, 'id': 119000, 'user_AboutMe': '<p>A bushpilot turned GIS Tech.  Learning on the job in Mining related GIS.</p>\n', 'user_AccountId': 1708451.0, 'user_DisplayName': 'cndnflyr', 'user_Reputation': 2990.0, 'user_Views': 206.0, 'user_id': 9248.0}}