How to get all contacts in Exchange Web Service (not just the first few hundreds)

丶灬走出姿态 提交于 2019-12-11 01:17:07

问题


I'm using Exchange Web Service to iterate through the contacts like this:

ItemView view = new ItemView(500);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);
FindItemsResults<Item> findResults = _service.FindItems(WellKnownFolderName.Contacts, view);
foreach (Contact item in findResults.Items)
{
  [...]
}

Now this restricts the result set to the first 500 contacts - how do I get the next 500? Is there some kind of paging possible? Of course I could set 1000 as Limit. But what if there are 10000? Or 100000? Or even more?


回答1:


You can do 'paged search' as explained here.

FindItemsResults contains a MoreAvailable probably that will tell you when you're done.

The basic code looks like this:

while (MoreItems)
{
// Write out the page number.
Console.WriteLine("Page: " + offset / pageSize);

// Set the ItemView with the page size, offset, and result set ordering instructions.
ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);
view.OrderBy.Add(ContactSchema.DisplayName, SortDirection.Ascending);

// Send the search request and get the search results.
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Contacts, view);

// Process each item.
foreach (Item myItem in findResults.Items)
{
    if (myItem is Contact)
    {
        Console.WriteLine("Contact name: " + (myItem as Contact).DisplayName);
    }
    else
    {
        Console.WriteLine("Non-contact item found.");
    }
}

// Set the flag to discontinue paging.
if (!findResults.MoreAvailable)
    MoreItems = false;

// Update the offset if there are more items to page.
if (MoreItems)
    offset += pageSize;
}   


来源:https://stackoverflow.com/questions/20188197/how-to-get-all-contacts-in-exchange-web-service-not-just-the-first-few-hundreds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!