Reputation: 161
I want to get the item object each time an appointment is removed in an outlook addin. The problem is that ItemRemoveEventHandler delegate does not return the item being removed contrary to ItemAddEventHandler or ItemChangeEventHandler. I've tried some codes snippets but none of them are working. A poor workaround is to attach an ItemAddEventHandler on the Trash Folder (Outlook.OlDefaultFolders.olFolderDeletedItems). That's not working either because the item is not moved to the trash folder but removed/added : so the EntryID (unique key identifier) is not the same...
Does anybody have a piece of code to get the item before being removed?
Upvotes: 2
Views: 1566
Reputation: 66276
These events fire 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 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 - 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.
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.
Upvotes: 2
Reputation: 2888
Try using Folder.BeforeItemMove
and checking the value of the parameter MoveTo
which is a MAPIFolder
. MoveTo.StoreID
should be the same as the default folder for deleted items. For a shift + delete (permanent delete) MoveTo
should equal null
.
private void BeforeItemMove(object Item, MAPIFolder MoveTo, ref bool Cancel)
{
AppointmentItem appointment = (AppointmentItem)Item;
Folder deletedItemsFolder =
(Folder)Application
.Session
.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
if(MoveTo == null || MoveTo.StoreID == deletedItemsFolder.StoreID)
{
// Do something...
}
Marshal.ReleaseComObject(deletedItemsFolder);
deletedItemsFolder = null;
Marshal.ReleaseComObject(appointment);
appointment = null;
}
I got this idea from the question BeforeItemMove event not fired in Outlook when item is deleted on remote machine.
Upvotes: 0