Search code examples
c#outlook-addin

Outlook Addin appointmentitem PropertyChange event firing too much


I'm creating an Outlook addin that needs to trigger when the attendees of a meeting are changed. The problem I'm encountering is that the PropertyChange event of an Appointment item is firing way too much and because of that I can't get the exact amount of attendees.

Here is some of the code I'm using:

private Outlook.AppointmentItem appointmentItem;
private void Inspectors_NewInspector(Outlook.Inspector inspector)
    {
        var item = inspector.CurrentItem as Outlook.AppointmentItem;
        if (item != null)
        {
            appointmentItem = item;
            temp = inspector;
            visible = false;
            appointmentItem.PropertyChange += AppOnPropertyChange;
        }
    }

private void AppOnPropertyChange(string name)
    {

        if (name.Equals("RequiredAttendees"))
        {
            var test = appointmentItem.Recipients.Count;
        }
    }

The event is triggered way too many times, and thus "test" will get values between 1 (the sender is always an attendee) and the number of attendees. Also if you have lets say 3 attendees and you remove one, "test" will be 4 - 3 - 2 - 1.

Is there a way to figure out the exact number of attendees in an AppointmentItem?

Any help would be much appreciated.


Solution

  • ItemChange("RequiredAttendees") will fire for any change in the required, optional, or resource attendees, there is nothing you an do about that.

    I had luck using a timer - when ItemChange event fires, enable the timer (you can set its interval to, say, 100 ms). When the timer event fires, disable the timer (so it won't fire again). and process the recipients collection. By that time all the changes will be processed and you will only process the attendees once.

    Since you are using .Net, use the Timer class from the Forms namespace (it works on the main thread) instead of the System namespace (it uses a background thread, which is a bad practice when working with Outlook objects in a COM addin).