Retrieving More than 50 records using OData in CRM 2011

Posted: December 7, 2012 in CRM 2011, Jscripts

When retrieving records using OData in MS CRM 2011 the page size is fixed to Max 50 records. So in my example I’m going to push the first 50 records in an array and retrieve next page results using the URI (“__next”) provided in the JSON response object.

In my case i’m going to retrieve records in Account entity. Here is the code:

relatedAccounts = [];

function onload() {
    var serverUrl = Xrm.Page.context.getServerUrl();
    var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/AccountSet?$select=AccountId,Name,&$filter=StateCode/Value eq 0";
    GetRecords(oDataUri);
    var totalRecords = relatedAccounts.length;
}

function GetRecords(url) {
    jQuery.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: url,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function (data, textStatus, XmlHttpRequest) {
            if (data && data.d != null && data.d.results != null) {
                AddRecordsToArray(data.d.results);
                FetchRecordsCallBack(data.d);
            }
        },
        error: function (XmlHttpRequest, textStatus, errorThrown) {
            alert("Error :  has occured during retrieval of the records ");
        }
    });
}

function AddRecordsToArray(records) {
    for (var i = 0; i < records.length; i++) {
        relatedAccounts.push(records[i]);
    }
}

function FetchRecordsCallBack(records) {
    if (records.__next != null) {
        var url = records.__next;
        GetRecords(url);
    }
}

urlrecords

TotalRecords

DetailedRecords

Advertisements
Comments
  1. Sudheera says:

    Hi Reddy,
    Thank you very much for posting here. Actually this solve my problem.

  2. Nice article.
    One Suggestion you could use concat() instead of manually adding data to array in your “AddRecordsToArray()” method.

  3. Saied Basha says:

    If I’m using javascript in a client side [offline html files], is there a way to authenticate Online CRM service calls?

  4. […] solution here I suggest you look at Lakshman Reddy's blog which is what lead me to my fix.https://lakshmanindian.wordpress.com/2012/12/07/retrieving-more-than-50-records-using-odata-in-crm-20…The only issue I had was the process of returning all those Roles slowed down my CRM page.  So […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s