Microsoft Exchange Folders.findItems结果限制为1000

时间:2012-08-30 15:13:15

标签: c# exchangewebservices

我正在尝试从microsoft exchange中的联系人文件夹中获取联系人列表 即使文件夹中有更多项目,结果也只返回1000个项目 这是我的代码。

FindFoldersResults r = service.FindFolders(new FolderId(WellKnownFolderName.PublicFoldersRoot), new FolderView(10));
     Folder folder = getFolder("test", r.Folders);
     ContactsFolder contactsfolder = ContactsFolder.Bind(service, new FolderId(folder.Id.ToString()), new PropertySet(BasePropertySet.IdOnly, FolderSchema.TotalCount));        
     FindItemsResults<Item> items = folder.FindItems(new ItemView(contactsfolder.TotalCount));   

如何让它返回所有项目?

3 个答案:

答案 0 :(得分:3)

正如Jason所引用的文章所说,分页是关键。以下是我对Office365 Exchange Server使用的代码,以获取给定文件夹中的所有电子邮件列表(超过20,000封电子邮件,工作速度非常快,页面大小为100):

            // via https://msdn.microsoft.com/en-us/library/office/dn592093(v=exchg.150).aspx
            int pageSize = 100;
            int offset = 0;
            ItemView view = new ItemView(pageSize + 1, offset);
            view.PropertySet = new PropertySet(ItemSchema.Subject, ItemSchema.DateTimeSent);
            view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
            view.Traversal = ItemTraversal.Shallow;

            bool moreItems = true;
            ItemId anchorId = null;

            while (moreItems)
            {
                FindItemsResults<Item> results = service.FindItems(buildsFolderId, view);
                moreItems = results.MoreAvailable;
                if (moreItems && anchorId != null)
                {
                    // Check the first result to make sure it matches
                    // the last result (anchor) from the previous page.
                    // If it doesn't, that means that something was added
                    // or deleted since you started the search.
                    if (results.Items.First<Item>().Id != anchorId)
                    {
                        Console.Error.WriteLine("The collection has changed while paging. Some results may be missed.");
                    }
                }

                if (moreItems)
                {
                    view.Offset += pageSize;
                }

                anchorId = results.Items.Last<Item>().Id;

                // Because you’re including an additional item on the end of your results
                // as an anchor, you don't want to display it.
                // Set the number to loop as the smaller value between
                // the number of items in the collection and the page size.
                int displayCount = results.Items.Count > pageSize ? pageSize : results.Items.Count;

                for (int i = 0; i < displayCount; i++)
                {
                    Item item = results.Items[i];

                    Console.WriteLine("[" + item.DateTimeSent + "] " + item.Subject);
                }

                Console.Error.WriteLine("Current offset: {0}/{1}", view.Offset, folder.TotalCount);
            }

答案 1 :(得分:1)

我发现这篇文章描述了可能导致限制为1000项的EWSFindCountLimit设置。

这似乎仅适用于Exchange Server 2010.

Throttling Policies and the EWSFindCountLimit

来自New-ThrottlingPolicy

Set-ThrottlingPolicy -Identity <ThrottlingPolicyIdParameter> [-EWSFindCountLimit <UInt32>] 

答案 2 :(得分:0)

以下是相同答案的PowerShell版本:

$allItems = @()

$pageSize = 1000
$offset = 0
$ItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView -ArgumentList ($pageSize + 1), $offset
$ItemView.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet ([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties,[Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::DateTimeReceived)
$ItemView.OrderBy.Add([Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::DateTimeReceived, 'Descending')
$SearchQuery = "sent:>=1/23/2020 AND sent:<=1/24/2020" 
$Folder = 'SentItems'

do
{
    $items = $exchangeService.FindItems($Folder, $SearchQuery, $ItemView)
    Write-Host "Items Count: $($items.Items.Count), offset: $($ItemView.Offset)"

    if ($items.MoreAvailable)
    {
        $ItemView.Offset += $pageSize
    }

    $items.Items | ForEach-Object {
        $allItems += $PSItem
    }
}
while ($items.MoreAvailable)

$allItems.Count