Use Of OData in Javascript in Microsoft Dynamics CRM

Hi Friends,

This will be oldest post you will find in google regarding odata usage in Microsoft Dynamics CRM , but i feel this will give lots of information regarding Odata. Although Microsoft has introduced WEB API in javascript in its latest update, till we can write our queries using OData .

XMLHttpRequest object :

Server requests from browser are made through XMLHttpRequest Object . JavaScript requests use XMLHttpRequest use two functions

  • Send Request – to send request to browser
  • Call Back – to get response browser .

XMLHttpRequest  object has capabilites to send requests to browser and call back (response) requests from browser .

Often we use jquery functions to write these queries , but Microsoft has recommended to user not to use Jquery methods for Querying data . (Here Jquery.Ajax is referred as $.Ajax which uses XMLHttpRequest available in the browser for querying the data from server )

PUT,DELETE,MERGE  requests are noted as POST request .

RETRIEVE request are stated as GET request.

To support versions of Windows Internet Explorer prior to Internet Explorer 7, use the following function to get the XMLHttpRequest object.

//Retrieves XMLHttpRequest object
function GetRequestObject()
{
if (window.XMLHttpRequest)
{
return new window.XMLHttpRequest;
} else
{
try {
return new ActiveXObject(“MSXML2.XMLHTTP.3.0”);
} catch (ex) {
return null;
}
}
}

 

Access the server URL :

The first thing to do when you start to use the ODATA endpoint with JavaScript is establish the URL to the organization root URL

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

Create OData Query :

It can be created manually or using CRM rest builder tool

Sample :

var userRolesQuery = “/RoleSet?$top=1&$filter=RoleId eq guid'” + RoleId + “‘&$select=Name”;

Example : Retrieve Security Roles of Logged in User in Microsoft Dynamics CRM

//Main method to retrive all user roles

function getAllUserRoles()
{
var Roles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < Roles.length; i++)
{
var RoleId = Roles[i];
//Query designed by using Odata
var userRolesQuery = “/RoleSet?$top=1&$filter=RoleId eq guid'” + RoleId + “‘&$select=Name”;
var role = null;
role = GetRolesRequest(userRolesQuery);
var RoleName = role[0].Name;

alert(“Role ID=” + RoleId + ” Role Name=” + RoleName);
}
}

NOTE : Xrm.Page.context.getUserRoles() returns an array of GUIDs of roles  that the User currently has

// OData Request :

function GetRolesRequest(query)
{
var serverUrl = Xrm.Page.context.getClientUrl();

var oDataEndpointUrl = serverUrl + “/XRMServices/2011/OrganizationData.svc/”;
oDataEndpointUrl += query;

var service = GetRequestObject();

if (service != null) {
service.open(“GET”, oDataEndpointUrl, false);
service.setRequestHeader(“X-Requested-With”, “XMLHttpRequest”);
service.setRequestHeader(“Accept”, “application/json, text/javascript, */*”);
service.send(null);

var retrieved = JSON.parse(service.responseText).d;

var results = new Array();
for (var i = 0; i < retrieved.results.length; i++) {
results.push(retrieved.results[i]);
}

return results;
}
return null;
}

 

Retrieve XMLHttpRequest  object :

//Retrieves XMLHttpRequest object
function GetRequestObject()
{
if (window.XMLHttpRequest)
{
return new window.XMLHttpRequest;
} else
{
try {
return new ActiveXObject(“MSXML2.XMLHTTP.3.0”);
} catch (ex) {
return null;
}
}
}

Hope this helps 🙂

Advertisements

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