paginationhttprequestodatadynamics-business-centralnext-link

Business Central Server-Driven Paging @odata.nextLink in c#


I'm trying to get some example code to transform this logic into one where it requests while there is @odata.nextLink. There seems to be 10k records in my http response, limiting by request.Headers.Add("Prefer", "odata.maxpagesize=1000"); What is happening is with my current code, it times out. Hoping using @odata.nextLink will solve the issue of my request taking too long. I know I need to put it in a do while or while loop while there until I return all my pages, so I assume it should take 10 requests. Thank you

Code:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Headers.Add("Prefer", "odata.maxpagesize=1000");
            request.ContentType = CONTENTTYPE;
            request.Method = GET_WebMethod;

            if (!String.IsNullOrEmpty(Authorization))
                request.Headers.Add(HttpRequestHeader.Authorization, Authorization);

            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                ContentResult = result;
            }

Return ContentResult;


Solution

  • Business Central has a fixed OData Page Size of 20,0000 records which cannot be changed.

    This means you will only get an @odata.nextLink when the result of your request contains more than 20,000 records.

    If you want less records in your response you could use a combination of the $top and $skip query parameters.

    You would loop until you get an empty response i.e., an empty JSON array.

    An example with REST Client in VS Code for the companies endpoint with 1000 records could be:

    get {{baseUrl}}/companies?$top=1000&$skip=0
    

    The next call would be:

    get {{baseUrl}}/companies?$top=1000&$skip=1000
    

    And then you just continue looping and incrementing the $skip value.