To get more than 50 records through ODATA Services

In Dynamics CRM, There is a page limit of 50 records through Odata services. To increase that we should use paging to get more than 50+ records.

//To get account records through Odata services

relatedAccounts = [];

function GetAllAccountsRecords() {
debugger;
    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/AccountSet?$select=AccountId,Name";
    GetRecords(oDataUri);
    var totalRecords = relatedAccounts.length;
   //alert("Total records :" +totalRecords);


}
 //Calling Odata
function GetRecords(url) {
    $.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 ");
        }
    });
}
//To get more records through Odata services
function AddRecordsToArray(records) {
    for (var i = 0; i < records.length; i++) {
        relatedAccounts.push(records[i]);
  }
}
//To get next page records through Odata services
function FetchRecordsCallBack(records) {
    if (records._next != null) {
        var url = records._next;
        GetRecords(url);
    }
}
//end of Odata services

No comments:

Post a Comment