'Outlook ItemAdd event fires twice for new Calendar items

I'm working on an Outlook add-in that will monitor the current user's calendar and send that user an email when a specific type of appointment or meeting is received. We have a third party app/service that is sending new meeting requests to user in Outlook, but no notification is given to the user logged into Outlook. My add-in is a workaround until we replace the third party app, so users can be alerted when this meeting request is sent.

I'm using the ItemAdd event to monitor when an appointment/meeting is added (i.e. sent from the third party app). What I'm seeing is that the event fires twice (even though I declared the handler only once): once when the appointment is received from a different user, and once when the appointment is accepted or tentatively accepted by the current user.

I need it to fire only when the appointment is at first received, not when it is accepted. I could monitor the user's inbox to see if they already received the notification, but I don't think this would work well if they haven't actually received the email before they click Accept (server latency?)

Here is my code. Any ideas would be greatly appreciated.

public partial class ThisAddIn
{
    Outlook.Items _Appointments = null;
    Outlook.Folder _MyAppointmentsFolder = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        // Initialization.
        _MyAppointmentsFolder = (Outlook.Folder)this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
        _Appointments = _MyAppointmentsFolder.Items;
        _Appointments.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(appointments_Add);
    }

    private void appointments_Add(object item)
    {
        // An appointment has been added. Read the title and send an email based on a condition.
        Outlook.AppointmentItem meetingItem = item as Outlook.AppointmentItem;
        if (meetingItem.Subject.Contains("Service Call"))
        {
            // Let's send ourselves an email.
            string emailTo = string.Format("{0}@concurrency.com", Environment.UserName);
            string subject = meetingItem.Subject;
            string body = meetingItem.Body;
            string startDate = meetingItem.Start.ToString();
            string endDate = meetingItem.End.ToString();

            SendEmailAlert(emailTo, subject, body, startDate, endDate);
        }

    }
    ....


Solution 1:[1]

If you assign the value for meetingItem.GlobalAppointmentID to a class level variable after sending the email, and check that value before sending, this should prevent the email from being sent twice. I've tested this method a bit and it seems to work well. Here is my updated code:

...

string _PreviousMeetingId = string.Empty; // class-level variable

...

private void appointments_Add(object item)
    {
        // An appointment has been added. Read the title and send an email based on a condition.
        Outlook.AppointmentItem meetingItem = item as Outlook.AppointmentItem;
        if (meetingItem.Subject.Contains("Service Call") && _PreviousMeetingId != meetingItem.GlobalAppointmentID)
        {
            // Let's send ourselves an email.
            string emailTo = string.Format("{0}@concurrency.com", Environment.UserName);
            string subject = meetingItem.Subject;
            string body = meetingItem.Body;
            string startDate = meetingItem.Start.ToString();
            string endDate = meetingItem.End.ToString();

            SendEmailAlert(emailTo, subject, body, startDate, endDate);

            // Save the ID of the meeting so we can check it later (above).
            _PreviousMeetingId = meetingItem.GlobalAppointmentID;
        }

    }

Solution 2:[2]

When a meeting request is received, Outlook creates a temporary tentative appointment. After you accept it, the first appointment is deleted and a new one is added, so it is no wonder the event fires twice.

You should see this same behavior in OutlookSpy (I am its author) if you go to the Calendar folder, click Folder button, select Items property, click Browse, go to the Events tab and look at the log at the bottom of the tab while receiving and accepting a meeting.

Solution 3:[3]

When a meeting request is received, Outlook creates a temporary tentative appointment. After you accept it, the first appointment is deleted and a new one is added, so it is no wonder the event fires twice.

Do you see the same behavior in OutlookSpy (I am its author) if you go to the Calendar folder, click Folder button, select Items property, click Browse, go to the Events tab and look at the log at the bottom of the tab?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul
Solution 2
Solution 3 Dmitry Streblechenko