Featured Post

Web API Requests Series

Web API Series:  Create and Retrieve , Update and Delete , Retrieve Multiple , Associate/Disassociate , Impersonation , Run Workflows

02 March 2016

CRM 2016 - WEB API CRUD Operations (Part 2)

Retrieving multiple records using paging with the new CRM 2016 Web API in Javascript

On my previous post I talked about how to "Retrieve" a single record by GUID and how to create a record.
In this post I will show you how to perform a simple "RETRIEVEMULTIPLE" using the new CRM Web API.

Straight to the code, which you can add to the other functions from my previous post.
The highlighted code is necessary to achieve paging in cases where we want to, for whatever reason, or when there are more than 5000 records in the result set.
(Note that is different from the previous implementation of the OData service)
If you do not want to do paging and you have more than 5000 records remove this line:
"req.setRequestHeader("Prefer", "odata.maxpagesize=500");" and every page will contain 5000 records not 50 as in the previous OData service.

this.retrieveMultipleRecords = function (entitySetName, select, successCallback, errorCallback, onComplete) {

        var req = new XMLHttpRequest();

//This part works, but I am not sure if it is the best way
        if (select.indexOf("skiptoken") != -1) {
            req.open("GET", select, true);
        else {
            req.open("GET", encodeURI(getWebAPIPath() + entitySetName + select), true);

        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Prefer", "odata.maxpagesize=500");

        req.onreadystatechange = function () {
            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null;                
                if (this.status == 200) {
                    var returned = JSON.parse(this.responseText, bd_Utilities.WebAPI.dateReviver);
                    if (successCallback) {

                    if (returned["@odata.nextLink"] != null) {
                        var queryOptions = returned["@odata.nextLink"];

                        bd_Utilities.WebAPI.retrieveMultipleRecords(entitySetName, queryOptions, successCallback, errorCallback, onComplete);
                    else {
                else {
                    if (errorCallback) {

And here is the function call:

retrieveMultiple: function () {
        "use strict";
        bd_Utilities.WebAPI.retrieveMultipleRecords("new_events", "?$select=new_name",

            function (results) {
                events = events.concat(results.value);
            function (error) { console.log(error.message); },

            function () {
                console.log("Retrieve multiple complete!");

No comments:

Post a Comment