We have had a few cases come up recently to do with issues implementing drag and drop in an Outlook add-in. There is an issue in Outlook 2010 that doesn’t release selected items once they have been dragged to a drop-target. This means that once the drag/drop operation has been completed, the items selected during the drag/drop will remain selected even if the user then selects others. When an action is taken on these selected items (move, delete, etc.), then the drag/drop items are also included in this selection even if this isn’t showing in the view. This can result in a poor user experience.
Unfortunately, this issue isn’t going to be fixed. Drag and drop is not a published feature for Outlook, and is therefore not supported. So, if you need to use drag and drop, you need to be aware of the issue and implement a workaround.
The simplest workaround currently is to switch to another folder view and then back again. Switching view releases all selected items. To implement this programmatically, however, may involve some creative thinking (the alternative is to educate users about the issue and ask that they manually change folders after a drag/drop). The actual implementation will vary slightly depending upon application, but the basic theory is the same in all cases – swap view to release items after any drag/drop operation.
In one particular case I wrote an example add-in that will automatically swap folder view to demonstrate how it can be done. It isn’t what I’d call particularly robust code, as it relies on a timer (there is no appropriate event to hook into in the Outlook Object Model). It does work though, and I have had reports back that in practise it works well. It also relies on an empty hidden folder to switch to (this is created automatically). The reason for the empty hidden folder is so that the view switch occurs as quickly as possible (to improve user experience). It would be possible to switch to an existing folder, but if there were many messages in it then this would be noticeable.
The example add-in (written in Visual Studio 2010) is attached to this article.
When compiled and run, the add-in shows a window that acts as a drag/drop target. You can drag items from a folder into the top half of the window (within the group box). I have also included a small application (DragDropTest.exe) that will trigger the Outlook bug (just drag some items onto the window that appears when you run that application).
When the fix is enabled, the drag/drop operation onto the group box will add the items to the listbox (i.e. process them) and also add them to a list. The view will be programmatically changed to the hidden folder (created when the add-in starts up) and then back again to the current folder. At this point, a timer is initiated. The timer simply attempts to reselect all the items that are in the list, and when this is successful (or it has timed out), it disables itself.
The actual nuts and bolts can be seen clearly from the code.