SharePoint oData API Only Returns 1000 Records

1.4k views Asked by At

I am trying to query a SharePoint 2013 list using the Rest API for all items in the list. The problem is it only returns 1000 records max and I need to get all of the records. I am using the oData v4 API and auto generated service references for the site.

I figured it out: I am including the question and answer here in case anyone else needs it.

2

There are 2 answers

0
Adam On BEST ANSWER

I created an extension method called SelectAll() that returns all of the records for a given query.

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

You use it by calling dataContext.SelectAll(query);

0
SolarX On

I had the same problem, and wanted it to be a generic solution without providing the query. I do use the EntitySetAttribute to determine the 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;
    }