SharePoint oData API仅返回1000条记录

时间:2015-06-17 13:33:35

标签: sharepoint odata sharepoint-2013 wcf-data-services

我正在尝试使用Rest API查询列表中所有项目的SharePoint 2013列表。问题是它最多只返回1000条记录,我需要获取所有记录。我正在使用oData v4 API和网站的自动生成的服务引用。

我明白了:我在这里提出问题和答案,万一其他人需要它。

2 个答案:

答案 0 :(得分: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);

来使用它

答案 1 :(得分: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;
    }