2015-06-17 85 views
0

我正在嘗試使用Rest API爲列表中的所有項目查詢SharePoint 2013列表。問題是它只會返回最多1000條記錄,我需要獲取所有記錄。我正在使用oData v4 API和爲網站自動生成的服務參考。SharePoint oData API僅返回1000條記錄

我想通了:我在這裏包括問題和答案,以防其他人需要它。

回答

0

我創建了一個名爲SelectAll()的擴展方法,它返回給定查詢的所有記錄。

public static List<T> SelectAll<T>(this DataServiceContext dataContext, IQueryable<T> query) 
{ 
    var list = new List<T>(); 
    DataServiceQueryContinuation<T> token = null; 
    var response = ((DataServiceQuery)query).Execute() as QueryOperationResponse<T>; 

    do 
    { 
     if (token != null) 
     { 
      response = dataContext.Execute(token); 
     } 

     list.AddRange(response); 

    } while ((token = response.GetContinuation()) != null); 

    return list; 
} 

通過調用dataContext.SelectAll(query);

0

使用它我有同樣的問題,並希望它是沒有將查詢提供一個通用的解決方案。我使用EntitySetAttribute來確定列表名稱。

public static List<T> GetAlltems<T>(this DataServiceContext context) 
    { 
     return context.GetAlltems<T>(null); 
    } 

    public static List<T> GetAlltems<T>(this DataServiceContext context, IQueryable<T> queryable) 
    { 
     List<T> allItems = new List<T>(); 
     DataServiceQueryContinuation<T> token = null; 

     EntitySetAttribute attr = (EntitySetAttribute)typeof(T).GetCustomAttributes(typeof(EntitySetAttribute), false).First(); 

     // Execute the query for all customers and get the response object. 
     DataServiceQuery<T> query = null; 

     if (queryable == null) 
     { 
      query = context.CreateQuery<T>(attr.EntitySet); 
     } 
     else 
     { 
      query = (DataServiceQuery<T>) queryable; 
     } 

     QueryOperationResponse<T> response = query.Execute() as QueryOperationResponse<T>; 

     // With a paged response from the service, use a do...while loop 
     // to enumerate the results before getting the next link. 
     do 
     { 
      // If nextLink is not null, then there is a new page to load. 
      if (token != null) 
      { 
       // Load the new page from the next link URI. 
       response = context.Execute<T>(token); 
      } 

      allItems.AddRange(response); 
     } 
     // Get the next link, and continue while there is a next link. 
     while ((token = response.GetContinuation()) != null); 

     return allItems; 
    }