EWS Managed API: Identify deleted email when fetching from "AllItems" folder

946 Views Asked by At

I am using EWS managed API with C# to fetch emails from user accounts. I am fetching the emails from "AllItems" folder and getting different email properties such as subject, datetimesent, etc.

"AllItems" folder also contains emails that are deleted and are in "DeletedItems" folder. I would like to identify whether the email is deleted (i.e. it is in "DeletedItems" folder) and if possible, when the email was deleted.

Below is the code I am using. I could not find a property that would identify whether the email is deleted.

FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };

ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };

SearchFilter folderFilter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "AllItems");

FolderId rootFolderId = new FolderId(WellKnownFolderName.Root);

FindItemsResults<Item> findResults;

FindFoldersResults AllItemsFolder= service.FindFolders(WellKnownFolderName.Root, folderFilter, viewFolders);

if (AllItemsFolder.Count() > 0)//if we have AllItems folder
{
    foreach (Folder folder in AllItemsFolder.Folders)
    {
        ItemView itv = new ItemView(int.MaxValue);
        findResults = service.FindItems(folder.Id, itv);

        foreach (Item item in findResults)
        {
            if (item is EmailMessage)
            {
                MessageBox.Show(item.Subject);
                //  Show whether the message is in deleted folder and when message was deleted  
            }
        }
    }
}
1

There are 1 best solutions below

2
On BEST ANSWER

As you state, I don't think there is a property like that for mail items.

I would use the GetFolder operation with the well known folder name "deleteditems" to get the Id of that folder. Then, I would ignore all mail items that has this Id as ParentFolderId.