How to get events of appointment items of outlook using c#

While creating outlook add-in many times we need to some manipulations when appointment item is added, changed or removed.

To do so we need to bind event handlers to item events.

Bellow is code to do so.

       public Microsoft.Office.Interop.Outlook.Application app = null;
       public Outlook.NameSpace ns = null;
       public Outlook.MAPIFolder calendar = null;
       public Outlook.Items appointments = null;
       public Outlook.AppointmentItem deleteItem;

       private void ThisAddIn_Startup(object sender, System.EventArgs e) {
          app = Application;
          ns = app.GetNamespace("mapi");
          ns.Logon("", "", true, true);
          calendar = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);

          appointments = calendar.Items;
          appointments.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(appointments_ItemAdd);
          appointments.ItemChange += new Outlook.ItemsEvents_ItemChangeEventHandler(appointments_ItemChange);
          appointments.ItemRemove += new Outlook.ItemsEvents_ItemRemoveEventHandler(appointments_ItemRemove);
       }

       private void appointments_ItemRemove() {
          // your logic
       }

       private void appointments_BeforeItemDelete(object item, ref bool cancel) {
          deleteItem = (Outlook.AppointmentItem) item;
          // your logic
       }

       private void appointments_ItemChange(object item) {
          // your logic
       }

       private void appointments_ItemAdd(object item) {
          ((Outlook.AppointmentItem) item).BeforeDelete += appointments_BeforeItemDelete;
          // your logic
       }

In addin Startup method I am binding item add, change & remove events.
In “appointments_ItemAdd” i am binding “appointments_BeforeItemDelete” with “BeforeDelete” of item so just before item is removed “BeforeDelete” event will be fired & will be save item in AppointmentItem field “deleteItem”.
We are doing this because “appointments_ItemRemove” is called after item is removed i.e. when “appointments_ItemRemove” is called at that time we will not have data of item which is removed, so to get data of item we are just saving it in field using “appointments_BeforeItemDelete” so that we can use it in “appointments_ItemRemove”.

admin