简体   繁体   中英

Outlook ContactItem DeleteEvent

Currently I´m writing a sync tool for GMail contacts and outlook, but there is a little problem: I need an event in my addin when the user deletes a contact, otherwise the sync tool would detect the missing contact on the outlook side and the tool will create the contact from the google side.

I´m accessing all Outlook contacts from the default folder with this code:

Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = GetCurrentNamespace();
Microsoft.Office.Interop.Outlook.MAPIFolder contacts = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

IList<Microsoft.Office.Interop.Outlook.ContactItem> items = new List<Microsoft.Office.Interop.Outlook.ContactItem>();

foreach (var contact in contacts.Items)
{
    items.Add(contact as Microsoft.Office.Interop.Outlook.ContactItem);
}

return items;

Edit 1: I already tried to subscribe to an BeforeDelete Event as John Saunders commented, but with no success. When I try to delete a contact in Outlook the event wont get fired.

Code:

Microsoft.Office.Interop.Outlook.NameSpace mapiNamespace = GetCurrentNamespace();
_contactMapiFolder = mapiNamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

//IList<Microsoft.Office.Interop.Outlook.ContactItem> items = new List<Microsoft.Office.Interop.Outlook.ContactItem>();
this._contacts = new List<Microsoft.Office.Interop.Outlook.ContactItem>();

foreach (var contact in _contactMapiFolder.Items)
{
    Outlook.ContactItem item = contact as Microsoft.Office.Interop.Outlook.ContactItem;
    item.BeforeDelete += ItemOnBeforeDelete;
    this._contacts.Add(item);
}

return this._contacts;

Can anybody provide me an example what events are available for such mapi (especially contact folders) folders are available and how they are working?

It is a really bad idea to set up an event sink on each and every item in a folder.

When Items.ItemRemove even fires, there is no way for you to figure out which item was deleted. You have no choice but to compare the current collection with what you have on the server or in some kind of local cache.

You can try to use Redemption and its RDOItems .ItemRemove event - it passes the value of the PR_INSTANCE_KEY MAPI property from the folder contents table. If you cache the values of the PR_INSTANCE_KEY property for all items ahead of time (you can use RDOITems.MAPITable.ExecSQL for that), you can figure out which item was deleted without looping through all items in the folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM