SharePoint oData API 只有 Returns 1000 条记录

SharePoint oData API Only Returns 1000 Records

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

我想出来了:我把问题和答案放在这里以防其他人需要。

我创建了一个名为 SelectAll() 的扩展方法,returns 给定查询的所有记录。

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