Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

c# - 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:[email protected]", iv);

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

    Console.WriteLine("Press Enter to continue");
    Console.ReadLine();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:" + 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:" + item.Subject);
            Console.WriteLine("Received At:" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
            Console.WriteLine("Is New:" + item.IsNew.ToString());
            Console.WriteLine("Has Attachments:" + item.HasAttachments.ToString());
            Console.WriteLine();
        }
    }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...