2013-11-25 69 views
2

我使用Exchange Web服務,通​​過這樣的接觸迭代在Exchange Web服務的所有聯繫人(不只是第一幾百):如何獲得

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) 
{ 
    [...] 
} 

現在,這將結果集限制到前500名聯繫人 - 我如何獲得下一個500名?有沒有可能的分頁?當然我可以設置1000作爲限制。但萬一有10000?還是100000?還是更多?

回答

3

你可以做'分頁搜索'爲explained here

FindItemsResults包含一個MoreAvailable可能會告訴你什麼時候完成。

基本的代碼如下所示:

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; 
}