SharePoint oData API仅返回1000条记录
我正在尝试使用Rest API为列表中的所有项目查询SharePoint 2013列表。问题是它最多只返回1000条记录,我需要获取所有记录。我正在使用ODataV4API和站点的自动生成服务引用SharePoint oData API仅返回1000条记录,sharepoint,odata,sharepoint-2013,wcf-data-services,Sharepoint,Odata,Sharepoint 2013,Wcf Data Services,我正在尝试使用Rest API为列表中的所有项目查询SharePoint 2013列表。问题是它最多只返回1000条记录,我需要获取所有记录。我正在使用ODataV4API和站点的自动生成服务引用 我找到了答案:我将问题和答案包含在这里,以备其他人需要。我创建了一个名为SelectAll()的扩展方法,该方法返回给定查询的所有记录 public static List<T> SelectAll<T>(this DataServiceContext dataContext,
我找到了答案:我将问题和答案包含在这里,以备其他人需要。我创建了一个名为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;
}
公共静态列表SelectAll(此DataServiceContext dataContext,IQueryable查询)
{
var list=新列表();
DataServiceQueryContinuation令牌=null;
var response=((DataServiceQuery)query).Execute()作为QueryOperationResponse;
做
{
if(令牌!=null)
{
response=dataContext.Execute(令牌);
}
列表。添加范围(响应);
}而((token=response.GetContinuation())!=null);
退货清单;
}
您可以通过调用
dataContext来使用它代码>我也有同样的问题,希望它是一个通用的解决方案,而不提供查询。我确实使用EntitySetAttribute来确定listname
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;
}
公共静态列表GetAllItems(此DataServiceContext上下文)
{
返回context.GetAlltems(null);
}
公共静态列表GetAllItems(此DataServiceContext上下文,IQueryable可查询)
{
列表所有项=新列表();
DataServiceQueryContinuation令牌=null;
EntitySetAttribute attr=(EntitySetAttribute)typeof(T).GetCustomAttributes(typeof(EntitySetAttribute),false).First();
//对所有客户执行查询并获取响应对象。
DataServiceQuery=null;
if(queryable==null)
{
query=context.CreateQuery(attr.EntitySet);
}
其他的
{
query=(DataServiceQuery)可查询;
}
QueryOperationResponse=query.Execute()作为QueryOperationResponse;
//使用来自服务的分页响应,使用do…while循环
//在获取下一个链接之前枚举结果。
做
{
//如果nextLink不为null,则有一个新页面要加载。
if(令牌!=null)
{
//从下一个链接URI加载新页面。
response=context.Execute(令牌);
}
allItems.AddRange(响应);
}
//获取下一个链接,并在有下一个链接时继续。
而((token=response.GetContinuation())!=null);
返回allItems;
}