【发布时间】:2015-06-17 13:33:34
【问题描述】:
我正在尝试使用 Rest API 为列表中的所有项目查询 SharePoint 2013 列表。问题是它最多只返回 1000 条记录,我需要获取所有记录。我正在为该站点使用 oData v4 API 和自动生成的服务参考。
我想通了:我把问题和答案写在这里,以防其他人需要。
【问题讨论】:
标签: sharepoint odata sharepoint-2013 wcf-data-services
我正在尝试使用 Rest API 为列表中的所有项目查询 SharePoint 2013 列表。问题是它最多只返回 1000 条记录,我需要获取所有记录。我正在为该站点使用 oData v4 API 和自动生成的服务参考。
我想通了:我把问题和答案写在这里,以防其他人需要。
【问题讨论】:
标签: sharepoint odata sharepoint-2013 wcf-data-services
我创建了一个名为 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);来使用它
【讨论】:
我有同样的问题,并希望它是一个通用的解决方案,而不提供查询。我确实使用 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;
}
【讨论】: