Posts Tagged ‘OData’

Sharing below code which returns some value using oData query. This function will check if there is any open activity related to Incident entity.

function CheckOpenActivity() {

var Id =;

var IncidentId = Id.substring(1, 37);

//Retrieve dynamically the organization’s server url

var¬†serverUrl = document.location.protocol +¬†“//”¬†+ +¬†“/”¬†+ Xrm.Page.context.getOrgUniqueName();

var¬†ODATA_ENDPOINT =¬†“/xrmservices/2011/OrganizationData.svc”;

var¬†ODATA_EntityCollection =¬†“/IncidentSet?$select=IncidentId,Incident_ActivityPointers/ActivityId,Incident_ActivityPointers/StateCode&$expand=Incident_ActivityPointers”;

var¬†ODATA_Filter1 =¬†“&$filter=IncidentId eq guid'”¬†+ IncidentId +¬†“‘”;

var oDataRequestUrl = serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + ODATA_Filter1;

var result = syncODataCall(oDataRequestUrl);

var isOpenActivity = ProcessReturnedEntities(result);

if (isOpenActivity)
alert(‘The Case has open Activity’);
alert(‘The Case has not any open Activity’);


// function to make synchronous oData call

function syncODataCall(odataSelect) {

var request = new XMLHttpRequest();“GET”, odataSelect,¬†false);


request.setRequestHeader(“Content-Type”,¬†“application/json; charset=utf-8”);


var objJQuery = jQuery.parseJSON(request.responseText);

return objJQuery.d


function ProcessReturnedEntities(d) {

var hasRelated = false;

if (d.results[0] != null) {

var activities = d.results[0].Incident_ActivityPointers;

if (activities != null) {

if (activities.results.length > 0) {

$.each(activities.results, function (index) {

if¬†(activities.results[index].StateCode.Value ==¬†‘0’) {

hasRelated = true;
return hasRelated;


} else {
hasRelated = false;
return hasRelated;

} else {
hasRelated = false;
return hasRelated;
return hasRelated;


Hope this help you.

Below is the sample of how to retrieve related entity’s field value using oData.

Let’s take a example ..suppose we want to retrieve some field value from SystemUser entity(User Name and EmployeeId) OnLoad of Account where OwnerId of Account is equal to SystemUserId of SystemUser entity.Write below¬† code on OnLoad of Account entity.

var serverUrl=Xrm.Page.context.getServerUrl();
var ownerId=Xrm.Page.getAttribute(“ownerid”).getValue()[0].id;
var ownerGuid=ownerId.substring(1,ownerId.length-1);

var oDataQuery=serverUrl+”/xrmservices/2011/OrganizationData.svc/SystemUserSet?$select=DomainName,EmployeeId,ParentSystemUserId,AccessMode&$expand=user_accounts&$filter=SystemUserId eq guid'”+ownerGuid+”‘”;


type: “GET”,
contentType: “application/json; charset=utf-8”,
datatype: “json”,
url: oDataQuery,
beforeSend: function(XMLHttpRequest)
success: function(data, textStatus, XmlHttpRequest)
if(data != null && data.d != null && data.d !=null)
var users=data.d.results[0];
var userName=users.DomainName;

//Lookup Manager(ParentSystemUserId)
var managerName=users.ParentSystemUserId.Name;
var managerId= users.ParentSystemUserId.Id;
var managerType=users.ParentSystemUserId.LogicalName;

// OptionSet
var accessMode=users.AccessMode.Value // This will return integer value

var employeeId=users.EmployeeId;
alert(‘User Name : ‘+users.DomainName +’Employee Id :’ +users.EmployeeId);
error: function(XmlHttpRequest,textStatus,errorThrown)
alert(‘oData select failed ‘+ oDataQuery);

Note : don’t forget to load json2.js and jquery1.4.1.min.js¬† framework on Account Form.

Hope it will help u.

Silverlight application can get a reference to the Xrm.Page Object instance using either of following approaches.

1. Using HTML bridge feature of Silverlight

ScriptObject ¬†xrm = (ScriptObject)HtmlPage.Window.GetProperty(‚ÄúXrm‚ÄĚ);

ScriptObject ¬†page= (ScriptObject)xrm.GetProperty(‚ÄúPage‚ÄĚ);

2. Using Dynamic Language Runtime

Using DLR you can utilize the dynamic language keywords which allow resolving method calls at runtime.

dynamic ¬†xrm = (ScriptObject)HtmlPage.Window.GetProperty(‚ÄúXrm‚ÄĚ);


Below is some important methods/property which is useful for Silverlight Application.

1. Getting the current Theme: This is useful if you are trying to style your Silverlight content in a similar way the user will see the web client. Valid Values are Default, Office12Blue, and Office14Silver.

var theme= xrm.Page.context.getCurrentTheme();

2.      Getting Org Name:

var orgName= xrm.Page.context.getOrgUniqueName();

3.      Getting the Server URL:

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

4.      Getting the User ID:

Knowing the user id that is working with the current page can be helpful when you need to do things like retrieve all the records that are owned by that person to present on the page.


var  userID = xrm.Page.context.getUserId();

5.      Getting the user’s Role:

var roles = xrm.Page.context.getUserRoles();

6.      Getting the entity logical Name:

String entityname;

7.      Getting the Entity Id:
Guid entityID =;

8.      Checking if Entity is Dirty:

By checking the dirty flag at the entity level you can quickly determine if there have been any changes to any of the fields. This doesn’t give you field level granularity you have to check each attribute if you need that.

bool isDirty =;

9.      Getting the Data as XML:

Using this feature you can get a string that represents the XML that would be sent to the server when the record is saved. The XML contains only the fields which have been modified.

String dataXml =;

10.  Saving Data to the Server:

The save function allows you to simulate saving data to the CRM server just like if the user hit the save button in the ribbon.

11.  Working with the Entity Attributes

You can get to the attributes on an entity via the Attributes collection. The different attribute types can have special methods that are only for their specific type.The following shows which methods each type of attribute currently has.

All Attribute have these methods:

addOnChange,fireOnChange, getAttributeType,getFormat,getInitialValue, getIsDirty, getName, getParent,getRequiredLevel, getSubmitMode, getUserPrivelege, getValue, removeOnChange, setRequiredLevel, setSubmitMode, and setValue

Money,decimal,integer and double have these methods too:

getMax, getMin, and getPrecision

Boolean and Optioset attributes have these methods:


Optionset attributes have these methods:

getOption, getOptions, getSelectedOption, and getText


12.  UI Methods:

The UI methods are high level methods located at and are the starting point for working with the UI controls. This is also the starting point for looking for Controls and Tabs.

Refreshing the Ribbon:

This method is beyond helpful if you are doing any enable/display rules that depends on values on the form. After the value is changed on the form you can use this method to force the ribbon to re-evaluate the data in the form so the ribbon is updated.


13.  Working with Form Controls:

The following methods are on all controls:

getControlType, getDisabled, getLabel, getName, getParent, setDisabled(all except web resources), setFocus, setLabel, and setVisible

The following methods are specific to Lookups;

addCustomeView, getDefaultView, and setDefaultView

The following methods are specific to Option Sets

adoption,clearOptions, and removeOption

The following methods are specific to Web Resources:

getData, getObject, setData, keep in mind the get/setData can only be used with Silverlight Web resources

The Following methods are specific to IFrames:

getSrc, setSrc, and getInitalUrl

The following methods are specific to Subgrids: