Advertisements
Home > CRM 2011, JScript > Retrieve multiple records synchronously using OData & JQuery in CRM 2011

Retrieve multiple records synchronously using OData & JQuery in CRM 2011

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
  1. Steve Ballmer
    July 18, 2012 at 8:52 AM

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

  2. Seamus
    March 9, 2016 at 8:29 PM

    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. No trackbacks yet.

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

%d bloggers like this: