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

Retrieve multiple records using OData & JQuery in CRM 2011


Below are the Jscript functions to retrieve multiple records using OData & JQuery.

function retrieveMultiple(odataSetName, select, filter, successCallback) {

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;


//Asynchronous AJAX function to Retrieve CRM records using OData


type: “GET”,

contentType: “application/json; charset=utf-8”,

datatype: “json”,

url: odataUri,

beforeSend: function (XMLHttpRequest) {

//Specifying this header ensures that the results will be returned as JSON.

XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);


success: function (data, textStatus, XmlHttpRequest) {

if (successCallback) {

if (data && data.d && data.d.results) {

successCallback(data.d.results, textStatus, XmlHttpRequest);


else if (data && data.d) {

successCallback(data.d, textStatus, XmlHttpRequest);


else {

successCallback(data, textStatus, XmlHttpRequest);




error: function (XmlHttpRequest, textStatus, errorThrown) {

if (XmlHttpRequest && XmlHttpRequest.responseText) {

alert(“Error while retrieval ; Error – ” + XmlHttpRequest.responseText);





function readRecordsOnSuccess(data, textStatus, XmlHttpRequest) {

// Loop through the retrieved records

for (var indx = 0; indx < data.length; indx++) {

alert(“Name – ” + data[indx].name);



  • Copy & Paste above methods 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” for an “Account”

function retrieveContactsByAccountId() {

// 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”;

// Read Account Guid

var accountId = Xrm.Page.data.entity.getId()

// Prepare filter

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

retrieveMultiple(oDataSetName, columns, filter, readRecordsOnSuccess);


Note :- Import “ODataQueryDesigner” solution to prepare the Filters easily.

Hope it helps 🙂

  1. Dylan Lawliet
    September 5, 2012 at 1:46 PM


    How can i get the optionset value in retrievemultiple?

    Thank you 🙂

  2. saber
    October 10, 2012 at 4:47 PM

    i have a mobile CRM application with html5 and javascript. When i execute the project in ie i get the popup for authentification and with google chrome doesn’t work ( no data retrieved).

    I wanna to know if there’s a solution to send the authentification parameters in the request??

  3. Sheenam
    November 26, 2012 at 3:02 PM

    How to pass multiple account id’s as a filter to retreive their attributes??

  4. Renan Albuquerque
    May 14, 2013 at 3:08 AM


    I’m creating an script to e-mail form entity. I need to retrieve on the field “FROM” the QUEUE based on the field “TO”.

    So when i fill the field TO, the script retrieve the contact and auto-complete the field “FROM” with the right Queue. To find the queue in C# i used the queries below: (How can i get in jscript exactly the same result ? Could you help me?)

    Contact resultContact = new Contact();

    query = new QueryByAttribute(Contact.EntityLogicalName);
    query.ColumnSet = new ColumnSet(“firstname”, “contactid”);
    query.AddAttributeValue(“emailaddress1”, email.Sender);
    var retrieved = service.RetrieveMultiple(query);

    resultContact = retrieved.Entities[0].ToEntity();

    var queryRecuperaConta = new QueryByAttribute(ava_account_contact.EntityLogicalName);
    queryRecuperaConta.ColumnSet = new ColumnSet(“contactid”, “accountid”);
    queryRecuperaConta.AddAttributeValue(“contactid”, resultContact.ContactId);
    var resultconta = service.RetrieveMultiple(queryRecuperaConta).Entities[0]. ToEntity();

    var queryRecuperaConta2 = new QueryByAttribute(Account.EntityLogicalName);
    queryRecuperaConta2.ColumnSet = new ColumnSet(“ava_filaid”, “name”, “accountid”);
    queryRecuperaConta2.AddAttributeValue(“accoutid”, resultconta.accountid);
    var conta = service.RetrieveMultiple(queryRecuperaConta2).Entities[0].ToEntity();

    var queryRecuperaFila = new QueryByAttribute(Queue.EntityLogicalName);
    queryRecuperaFila.ColumnSet = new ColumnSet { AllColumns = true };
    queryRecuperaFila.AddAttributeValue(“queueid”, conta.ava_filaid);
    var resultFila = service.RetrieveMultiple(queryRecuperaConta2).Entities[0].ToEntity();

    var queryTeam = new QueryByAttribute(Team.EntityLogicalName);
    queryTeam.ColumnSet = new ColumnSet { AllColumns = true };
    queryTeam.AddAttributeValue(“teamid”, resultFila.OwnerId);
    queryTeam.AddAttributeValue(“queueid”, resultFila.QueueId);
    var resultTeam = service.RetrieveMultiple(queryTeam).Entities[0].ToEntity();

    var FilaPrincipal = resultTeam.QueueId.Name;


  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 )

Connecting to %s

%d bloggers like this: