'How to know which Outlook Calendar item was deleted? [duplicate]
The following code alerts when new Outlook Calendar items are created and/or deleted:
static void Main(string[] args)
{
var oApp = new Application();
var mapiNamespace = oApp.GetNamespace("MAPI");
var CalendarFolder = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
var outlookCalendarItems = CalendarFolder.Items;
outlookCalendarItems.ItemAdd += item => Console.WriteLine($"Created: {(item as AppointmentItem).Subject}");
outlookCalendarItems.ItemRemove += () => Console.WriteLine($"Removed: ...?");
Console.WriteLine("Done.");
Console.ReadLine();
}
The problem is that outlookCalendarItems.ItemRemove
doesn't provide the actual item that was deleted...
How can I know which item was deleted (e.g. tell its subject)?
Solution 1:[1]
This event fires asynchronously so by the time the ItemRemove
event fires, the item is already gone. Even on the MAPI level, when the folder contents table fires the fnevTableModified | TABLE_ROW_DELETED
notification, it only provides the value of the PR_INSTANCE_KEY
property (you can see it in OutlookSpy (I am its author) - click IMAPIFolder, go to the GetContentsTable table, see the log at the bottom of the page when you delete an item).
If using Redemption is an option (I am also its author), you can use RDOItems.ItemRemove
event - it does pass the value of PR_INSTANCE_KEY
as the parameter.
PR_INSTANCE_KEY
is useful only if you have already cached the value of that property for all items in the folder, or at least of the items that you are interested in. Keep in mind that PR_INSTANCE_KEY cannot be cached between sessions - it is only valid for a particular instance of the contents table (IMAPITable
).
Also keep in mind that all items events are designed for the UI purposes only, they should not be used for any kind of synchronization - they can be and are dropped under heavy loads.
If you use the event for synchronization, your options are either to use ItemRemove
event as a hint that your sync must run sooner rather than later and loop through all items in a store to figure out which item changed, or, in case of Exchange, you can use the Incremental Change Synchronization API (ICS). It is exposed as the RDOFolderSynchronizer object in Redemption.
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 |