How to detect item removed from DeletedItems?

Add-in Express™ Support Service
That's what is more important than anything else

How to detect item removed from DeletedItems?
 
Byung Kun Kim


Guest


I added some customUserProperty to outlook drafts mail.

I want to do some additional tasks when user deletes the drafts mail, reading my added customproperty and do some tasks with the data in userproperty.

How can I achieve this? OutlookItemsEvents class doesn't seem to have ItemObj like arguments.

Can I know EntryID when mailitem deleted?

Perhaps I'm able to know drafts mail is moved to deleteItems folder by 'ProcessBeforeItemMove'. but how do I know mailitem deleted from deleteditems?


    Public Overrides Sub ItemRemove(ByVal SourceFolder As Object)
        'TODO: Add some code

        Dim outlookApp As Outlook._Application = DirectCast(Me.[Module], AddinModule).OutlookApp
        Dim targetFolder As Outlook.MAPIFolder = DirectCast(Me.FolderObj, Outlook.MAPIFolder)
        Dim journalItem As Outlook._JournalItem = Nothing

        'Try
        '    journalItem = DirectCast(outlookApp.CreateItem(Outlook.OlItemType.olJournalItem), Outlook._JournalItem)
        '    journalItem.Subject = [String].Format("You've removed items from {0}", targetFolder.FolderPath)
        '    journalItem.Save()
        'Finally
        '    If journalItem IsNot Nothing Then
        '        Marshal.ReleaseComObject(journalItem)
        '    End If
        'End Try

    End Sub





        public override void ProcessBeforeItemMove(object item, object moveTo, AddinExpress.MSO.ADXCancelEventArgs e)
        {
            if (item is Outlook._ContactItem)
            {
                Outlook._ContactItem movedContact = (Outlook._ContactItem)item;
                Outlook._Application outlookApp = ((AddinModule)this.Module).OutlookApp;
                Outlook._JournalItem journalItem = null;

                try
                {
                    journalItem = (Outlook._JournalItem)outlookApp.CreateItem(Outlook.OlItemType.olJournalItem);
                    if (moveTo != null)
                    {
                        Outlook.MAPIFolder targetFolder = (Outlook.MAPIFolder)moveTo;
                        journalItem.Subject = String.Format("You've moved {0} to {1}",
                            movedContact.FullName, targetFolder.FolderPath);
                        Marshal.ReleaseComObject(moveTo);
                    }
                    else
                    {
                        journalItem.Subject = String.Format("You've permanently deleted {0}",
                            movedContact.FullName);
                    }
                    journalItem.Save();
                }
                finally
                {
                    if (journalItem != null)
                        Marshal.ReleaseComObject(journalItem);
                }
            }
        }
Posted 11 Nov, 2016 00:32:53 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Hello Byung,

First off, you need to intercept the Folder.BeforeItemMove event; in Add-in Express, it is mapped to the BeforeItemMove method of the ADXOutlookItemsEvents class. The event allows you to learn the item being moved and the target folder, which is null if the item is "hard" deleted (via Shift+Delete). You can use a custom named property to identify "your" item. You need to study the following pages to learn how to create such a property:
- https://msdn.microsoft.com/EN-US/library/office/jj973068.aspx
- https://msdn.microsoft.com/en-us/library/office/ff863046.aspx
- https://social.msdn.microsoft.com/Forums/vstudio/en-US/08ecce0a-be0a-4d42-9bbd-7d1691b8abde/using-propertyaccessor-to-set-and-get-properties-in-a-meeting-request?forum=vsto

Using the ItemRemove event to detect deleting "your" item is difficult: the event doesn't provide a means to learn the item deleted and this means you need to manage a list of item in the folder and check this list when the ItemRemove event occurs.


Andrei Smolin
Add-in Express Team Leader
Posted 11 Nov, 2016 01:35:15 Top
Byung Kun Kim


Guest


Thank you for your favorable reply, Andrei. I'll study the Properties.


Andrei said:
which is null if the item is "hard" deleted (via Shift+Delete).


Does the same event occurs when user emptied deletedItems folder (via right click context menu) or just delete a mail(via non-Shift + Delete)?
Posted 11 Nov, 2016 02:20:57 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Yes, the same event occurs.

To study Outlook events, you can install our Outlook Events Logger add-in; see https://www.add-in-express.com/creating-addins-blog/2010/07/01/outlook-events-tool/. You need to install it, copy the source code to a location, uninstall it, rebuild the source code with the current Add-in Express version and register the add-in.


Andrei Smolin
Add-in Express Team Leader
Posted 11 Nov, 2016 03:47:19 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
Byung,

I would use the ItemSend event in both cases. What bothers me with the item-level Send event (such as the MailItem.Send event) is the fact that the send process may be cancelled after this event is fired.

On the other hand, at https://social.msdn.microsoft.com/Forums/office/en-US/e7b6ad40-9758-4d92-8abe-71a6e21fad77/calling-mailitemsave-in-itemsend-handler-causes-crash?forum=outlookdev, Ken Slovak writes:

===
Application_ItemSend() handlers don't work well with Simple MAPI sends as the event occurs too late in the process.
===

Simple MAPI inspectors are opened when you choose the Send command in other Office applications and in the Windows Explorer. If your code is supposed to handle such a scenario, your only choice is the item-level Send event.


Andrei Smolin
Add-in Express Team Leader
Posted 11 Nov, 2016 04:20:29 Top
Byung Kun Kim


Guest


Thank you my teacher.
Posted 11 Nov, 2016 04:47:33 Top
Andrei Smolin


Add-in Express team


Posts: 18793
Joined: 2006-05-11
You are welcome!


Andrei Smolin
Add-in Express Team Leader
Posted 11 Nov, 2016 04:51:02 Top