Search code examples
c#outlookoffice-interopoutlook-2010

Find Outlook AppointmentItem for ContactItem


I am creating ContactItem from code using Microsoft.Office.Interop.Outlook. This works absolutely fine. If I assign a the property Birthday, an corresponding AppointmentItem is created automatically. This is also fine.

But now when I delete the ContactItem, the AppointmentItem stays. This is obviously not what I was going for.

localContactToDelete.Delete();

Is there a way to retrieve the associated AppointmentItem in order to delete it manually?

I read that it should be possible (see below), however I do not find the propoerties or whatever.

http://social.msdn.microsoft.com/Forums/office/en-US/6b481f74-8422-46d4-90a9-a5860dcb98b5/to-avoid-automatic-birthday-calendar-event-creation-when-creating-contact?forum=outlookdev


Solution

  • Thanks to Dmitry's tip for using OutlookSpy to identify the NameSchema of the needed property I came up with the following code. It's definitely not ready, but at least works for the moment. Any suggestions welcome.

    Microsoft.Office.Interop.Outlook.Application outlookObject = new Microsoft.Office.Interop.Outlook.Application();
    
    MAPIFolder contactFolder = outlookObject.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
    Items contacts = contactFolder.Items;
    

    Then comes the stuff selecting the contact, e.g. a foreach.

    ContactItem contact;
    
    PropertyAccessor pa = contact.PropertyAccessor;
    Byte[] ba = pa.GetProperty("http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/804D0102");
    string birthdayAppointmentItemID = BitConverter.ToString(ba).Replace("-", string.Empty);
    
    NameSpace ns = outlookObject.GetNamespace("MAPI");
    AppointmentItem birthdayAppointmentItem = ns.GetItemFromID(birthdayAppointmentItemID);