Hi,

Below is the Jscript function to retrieve multiple records synchronously using OData & JQuery.

function retrieveMultipleSync(odataSetName, select, filter) {

var serverUrl = Xrm.Page.context.getServerUrl();

var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;

var odataUri = serverUrl + ODATA_ENDPOINT + “/” + odataSetName + “?”;

if (select) {

odataUri += “$select=” + select ;

}

if (filter) {

odataUri += “&” + “$filter=” + filter;

}

var service = GetRequestObject();

if (service != null) {

service.open(“GET”, odataUri, false);

service.setRequestHeader(“X-Requested-Width”, “XMLHttpRequest”);

service.setRequestHeader(“Accept”, “application/json,text/javascript, */*”);

service.send(null);

var requestResults = eval(‘(‘ + service.responseText + ‘)’).d;

return requestResults;

}

}

function GetRequestObject() {

var req = false;

if (window.XMLHttpRequest) {

req = new XMLHttpRequest();

} else if (window.ActiveXObject) {

req = new ActiveXObject(“Microsoft.XMLHTTP”);

}

return req;

}

  • Copy & Paste above method to a new .jscript file
  • Replace double quotes(“) symbols with your keyboard double quotes
  • Add the new .jscript file as web resource to CRM application
  • Add “Jquery” & json” helper classes as web resources

How do I call this method :-

  • In my sample below, I will show you how to read “Contacts” of an “Account”

function retrieveContactsByAccountId(accountId) {

// Pass ‘Contact’ set name since we are reading Contacts

var oDataSetName = “ContactSet”;

// Column names of ‘Contact’ (Pass * to read all columns)

var columns = “FirstName,LastName”;

// Prepare filter

var filter = “AccountId/Id eq guid’” + accountId + “‘”;

var requestResults  = retrieveMultipleSync(oDataSetName, columns, filter);

if (requestResults != null) {

if (requestResults.results != null && requestResults.results.length > 0) {

for (var indxContacts = 0; indxContacts < requestResults.results.length; indxContacts++) {

var contact = requestResults.results[indxContacts];

if (contact.FirstName) {

alert(“Contact’s First Name – ” + contact.FirstName);

}

}

}

}

else {

alert(“Error while retrieving; Error – ” + service.responseText);

}

}

Get OData URI conventions from here

🙂

Advertisements
Advertisements

4 responses to “Retrieve multiple records synchronously using OData & JQuery in CRM 2011”

  1. Steve Ballmer Avatar
    Steve Ballmer

    thanks mate, the asychronous method is next to useless in real environments.

  2. Seamus Avatar
    Seamus

    Thanks for this – really helpful. However, it only retrieves a max of 50 records. Is there any way to overcome this odata limitation? Thanks

    1. Rajeev Pentyala Avatar

      Hi,

      You can use Pagination approach to retrieve more than 50 records. Refer below link for sample code. http://community.dynamics.com/crm/b/crmanswers/archive/2015/03/30/javascript-odata-pagination-with-synchronous-calls

      1. Seamus Avatar
        Seamus

        Dear Rajeev,

        Thanks so much for this – you have really helped me get this sorted.

Leave a comment