Search code examples
c#exchange-serverexchangewebservices

Exchange Web Services (EWS) FindItems within All Folders


I am using the following code to find all the emails sent from a user, however this only searches the main Inbox folder and doesn't check any sub-folders. I would like to search all the mail items including any sub-folders.

I have tried the WellKnownFolderName.Root and WellKnownFolderName.Inbox, and these only search those folders, not the sub-folders.

private static void SearchItems(string email)
{
    ItemView iv = new ItemView(10);
    FindItemsResults<Item> fiitems = _service.FindItems(WellKnownFolderName.Inbox, "from:username@example.com", iv);

    foreach (Item item in fiitems)
    {
        Console.WriteLine("Subject:\t" + item.Subject);
        Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
        Console.WriteLine();
    }

    Console.WriteLine("Press Enter to continue");
    Console.ReadLine();
}

Solution

  • I have found some information on the AllItems folder within Exchange over at Glen's blog. I have ported the PowerShell script to C# as shown below.

    private static void SearchItems()
    {
        ExtendedPropertyDefinition allFoldersType = 
            new ExtendedPropertyDefinition(13825, MapiPropertyType.Integer);
    
        FolderId rootFolderId = new FolderId(WellKnownFolderName.Root);
        FolderView folderView = new FolderView(1000);
        folderView.Traversal = FolderTraversal.Shallow;
    
        SearchFilter searchFilter1 = new SearchFilter.IsEqualTo(allFoldersType, "2");
        SearchFilter searchFilter2 = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "allitems");
    
        SearchFilter.SearchFilterCollection searchFilterCollection = 
            new SearchFilter.SearchFilterCollection(LogicalOperator.And);
        searchFilterCollection.Add(searchFilter1);
        searchFilterCollection.Add(searchFilter2);
    
        FindFoldersResults findFoldersResults = 
            _service.FindFolders(rootFolderId, searchFilterCollection, folderView);
    
        if (findFoldersResults.Folders.Count > 0)
        {
            Folder allItemsFolder = findFoldersResults.Folders[0];
            Console.WriteLine("Folder:\t" + allItemsFolder.DisplayName);
    
            ItemView iv = new ItemView(1000);
            FindItemsResults<Item> findResults = 
                allItemsFolder.FindItems("System.Message.DateReceived:01/01/2011..01/31/2011", iv);
    
            foreach (Item item in findResults)
            {
                Console.WriteLine("Subject:\t" + item.Subject);
                Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
                Console.WriteLine("Is New:\t\t" + item.IsNew.ToString());
                Console.WriteLine("Has Attachments:\t\t" + item.HasAttachments.ToString());
                Console.WriteLine();
            }
        }
    
        Console.WriteLine("Press Enter to continue");
        Console.ReadLine();
    }