though I have mentioned that this code will work for both CRM 2011 and CRM 2013, but only in one place we have used getClientUrl() call, which is CRM 2013 specific, for more details on this please see my previous blog (https://sanghams.wordpress.com/2014/01/26/crm-2013-new-and-beautiful-getclienturl-method-instead-of-getserverurl/).
hence in the below code snippet, I will comment the script that will work for CRM 2011. so when you try this script in CRM 2011, just comment the statement that uses “getClientUrl” and uncomment the code, which I mention as to work for CRM 2011.
/*global window */
/*global Xrm */
/*jslint evil: true*/
/*global ActiveXObject:false */
function
getODataOrgUrl() {
‘use strict’;
/*//Uncomment Below 3 lines of code if you are using CRM 2011
// the below two lines to be used for crm 2011 onpremise
var orgname = window.parent.Xrm.Page.context.getOrgUniqueName();
var serverUrl = document.location.protocol + “//” + document.location.host + “/”;
// if crm 2011 online use the below code
// var serverUrl = window.parent.Xrm.Page.context.getServerUrl();
return (serverUrl + “/XRMServices/2011/OrganizationData.svc/”);*/
// Getting the CRM Org URL ** comment the below code if you are using CRM 2011 and use the previous 3 lines of code
return (Xrm.Page.context.getClientUrl() + “/XRMServices/2011/OrganizationData.svc/”);
}
function
getRequestObject() {
‘use strict’;
if (window.XMLHttpRequest) {
returnnew window.XMLHttpRequest();
}
try {
returnnew ActiveXObject(“MSXML2.XMLHTTP.3.0”);
}
catch (ex) {
returnnull;
}
}
function
getODataResults(query) {
‘use strict’;
var oDataEndpointUrl, service, requestResults;
oDataEndpointUrl = getODataOrgUrl();
service = getRequestObject();
requestResults =
null;
oDataEndpointUrl += query;
if (service !== null) {
service.open(
“GET”, oDataEndpointUrl, false);
service.setRequestHeader(
“X-Requested-Width”, “XMLHttpRequest”);
service.setRequestHeader(
“Accept”, “application/json,text/javascript, */*”);
service.send(
null);
if (service.responseText === null) {
returnnull;
}
requestResults = eval(
‘(‘ + service.responseText + ‘)’).d;
if (requestResults === null || requestResults.results === null) {
returnnull;
}
if (requestResults.results.length === 0) {
returnnull;
}
if (requestResults !== null && requestResults.results.length === 1) {
return requestResults.results[0];
}
}
}