How to check which version of CRM I'm using with the Help of Javascript?

Question Status

Verified
AnkitSharma asked a question on 28 Jul 2015 4:45 AM

Hi,

I'm developing a code in which i need to know if the version i'm using is CRM 2011/13/15.

So anyone Help me out with some code.

Reply
Suggested Answer
Anil Antony responded on 28 Jul 2015 4:53 AM

I dont think we got any straightforward option to check the version using javascript.

You may be able to use some logic from the following link

stackoverflow.com/.../how-to-get-server-version-crm-2011-or-crm-2013-using-javascript

thanks

Anil

Reply
Suggested Answer
Mahender responded on 28 Jul 2015 4:53 AM

Hi,

Actually there is no direct method to check this, but you can pick of some of the js methods which is added in new versions and some which is depreciated from older versions, similar to this: stackoverflow.com/.../how-to-get-server-version-crm-2011-or-crm-2013-using-javascript

Reply
Rajkumar Rajaraman responded on 28 Jul 2015 4:57 AM

There is no direct way to check this:

Refer the following link:

mileyja.blogspot.in/.../get-version-number-of-microsoft.html

Hope this helps.

Regards,

R.Rajkumar

"Please mark my answer as verified if you found it helpful"

Reply
AnkitSharma responded on 28 Jul 2015 6:27 AM

Hi Rajkumar,

i have tried that code.But it's Giving me some error ,will you help me out?

Reply
Rajkumar Rajaraman responded on 28 Jul 2015 6:30 AM

share the code here

Reply
AnkitSharma responded on 28 Jul 2015 6:37 AM

if (typeof (SDK) == "undefined")

{ SDK = { __namespace: true }; }

//This will establish a more unique namespace for functions in this library. This will reduce the

// potential for functions to be overwritten due to a duplicate name when the library is loaded.

SDK.SAMPLES = {

   _getServerUrl: function () {

       var OrgServicePath = "/XRMServices/2011/Organization.svc/web";

       var serverUrl = "";

       if (window.parent.Xrm.Page.data == null || window.parent.Xrm.Page.data == undefined) {

           _serverUrl = Xrm.Page.context.getClientUrl();

       }

       else {

           _serverUrl = window.parent.Xrm.Page.context.getClientUrl();

       }

       return serverUrl + OrgServicePath;

   },

   RetrieveVersionRequest: function () {

       var requestMain = ""

       requestMain += "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";

       requestMain += "  <s:Body>";

       requestMain += "    <Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">";

       requestMain += "      <request i:type=\"b:RetrieveVersionRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">";

       requestMain += "        <a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\" />";

       requestMain += "        <a:RequestId i:nil=\"true\" />";

       requestMain += "        <a:RequestName>RetrieveVersion</a:RequestName>";

       requestMain += "      </request>";

       requestMain += "    </Execute>";

       requestMain += "  </s:Body>";

       requestMain += "</s:Envelope>";

       var req = new XMLHttpRequest();

       req.open("POST", SDK.SAMPLES._getServerUrl(), true)

       req.setRequestHeader("Accept", "application/xml, text/xml, */*");

       req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");

       req.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute&quot;);

       var successCallback = null;

       var errorCallback = null;

       req.onreadystatechange = function () { SDK.SAMPLES.RetrieveVersionResponse(req, successCallback, errorCallback); };

       req.send(requestMain);

   },

   RetrieveVersionResponse: function (req, successCallback, errorCallback) {

       if (req.readyState == 4) {

           if (req.status == 200) {

               alert(req.responseXML.xml);

           }

           else {

               errorCallback(SDK.SAMPLES._getError(req.responseXML));

           }

       }

   },

   _getError: function (faultXml) {

       var errorMessage = "Unknown Error (Unable to parse the fault)";

       if (typeof faultXml == "object") {

           try {

               var bodyNode = faultXml.firstChild.firstChild;

               for (var i = 0; i < bodyNode.childNodes.length; i++) {

                   var node = bodyNode.childNodes[i];

                   if ("s:Fault" == node.nodeName) {

                       for (var j = 0; j < node.childNodes.length; j++) {

                           var faultStringNode = node.childNodes[j];

                           if ("faultstring" == faultStringNode.nodeName) {

                               errorMessage = faultStringNode.text;

                               break;

                           }

                       }

                       break;

                   }

               }

           }

           catch (e) { };

       }

       return new Error(errorMessage);

   },

   __namespace: true

};

Reply
AnkitSharma responded on 28 Jul 2015 6:39 AM

Reply
Rajkumar Rajaraman responded on 28 Jul 2015 6:51 AM

Use this,

errorCallback(SDK.SOAPSamples._getError(req.responseXML));

instead of

errorCallback(SDK.SAMPLES._getError(req.responseXML));

Refer this also:

https://msdn.microsoft.com/en-in/library/gg594434.aspx

Reply
AnkitSharma responded on 28 Jul 2015 7:05 AM

i don't think just to rename namespace is going to do any good with the error

Reply
Guido Preite responded on 28 Jul 2015 7:21 AM

Any reason why you want to do it with a SOAP call (RetrieveVersionRequest) instead of 2 IF as the stackoverflow example Anil Antony and HIMBAP linked?

Reply
AnkitSharma responded on 28 Jul 2015 7:32 AM

hi Guido,

Actually in my code,i was trying to get all the products which are active/Inactive using fetchXML.Now when I'm Testing My code on CRM 2015 i'm getting all the products

But in CRM 2015 Product have 4 status Active,Draft,Retired and Under Revision where In Crm 2011/2013 there are ony two i.e. Active & Inactive.So what i want to do is to get the version of CRM and accordingly set filter on my fetchXml

Reply
Suggested Answer
Guido Preite responded on 28 Jul 2015 7:48 AM

I asked why you want to do a SOAP call (RetrieveVersionRequest) instead of two IF conditions to check the CRM version, I didn't ask what you will do after you have the CRM version.

var version = 2011;
if (Xrm.Page.ui.setFormNotification !== undefined && Xrm.Page.context.isOutlookClient == undefined) { version = 2015; }
else { if (Xrm.Page.ui.setFormNotification !== undefined) { version = 2013; } }
alert(version);


Reply
AnkitSharma responded on 28 Jul 2015 8:15 AM

Dear Guido,

var version = 2011;

if (Xrm.Page.context.getIsAutoSaveEnabled !== undefined) { version = 2015; }

else { if (Xrm.Page.ui.setFormNotification !== undefined) { version = 2013; } }

Xrm.Page.context.getIsAutoSaveEnabled is not undefined in CRM2013 also

Reply
Verified Answer
Guido Preite responded on 28 Jul 2015 8:21 AM

EDIT: I checked and as you found the page reports wrong

msdn.microsoft.com/.../gg334511.aspx

getIsAutoSaveEnabled

This method is new for Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update

in fact the method works also with 2013 (at least in my 2013 UR2 instance)

I updated my previous code with another function, I repost here:

var version = 2011;
if (Xrm.Page.ui.setFormNotification !== undefined && Xrm.Page.context.isOutlookClient == undefined) { version = 2015; }
else { if (Xrm.Page.ui.setFormNotification !== undefined) { version = 2013; } }
alert(version);


Reply
Guido Preite responded on 28 Jul 2015 8:50 AM

updated my answer

Reply
Verified Answer
Guido Preite responded on 28 Jul 2015 8:21 AM

EDIT: I checked and as you found the page reports wrong

msdn.microsoft.com/.../gg334511.aspx

getIsAutoSaveEnabled

This method is new for Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update

in fact the method works also with 2013 (at least in my 2013 UR2 instance)

I updated my previous code with another function, I repost here:

var version = 2011;
if (Xrm.Page.ui.setFormNotification !== undefined && Xrm.Page.context.isOutlookClient == undefined) { version = 2015; }
else { if (Xrm.Page.ui.setFormNotification !== undefined) { version = 2013; } }
alert(version);


Reply
Suggested Answer
Anil Antony responded on 28 Jul 2015 4:53 AM

I dont think we got any straightforward option to check the version using javascript.

You may be able to use some logic from the following link

stackoverflow.com/.../how-to-get-server-version-crm-2011-or-crm-2013-using-javascript

thanks

Anil

Reply
Suggested Answer
Mahender responded on 28 Jul 2015 4:53 AM

Hi,

Actually there is no direct method to check this, but you can pick of some of the js methods which is added in new versions and some which is depreciated from older versions, similar to this: stackoverflow.com/.../how-to-get-server-version-crm-2011-or-crm-2013-using-javascript

Reply
Suggested Answer
Guido Preite responded on 28 Jul 2015 7:48 AM

I asked why you want to do a SOAP call (RetrieveVersionRequest) instead of two IF conditions to check the CRM version, I didn't ask what you will do after you have the CRM version.

var version = 2011;
if (Xrm.Page.ui.setFormNotification !== undefined && Xrm.Page.context.isOutlookClient == undefined) { version = 2015; }
else { if (Xrm.Page.ui.setFormNotification !== undefined) { version = 2013; } }
alert(version);


Reply
Suggested Answer
Chitrarasan Duraisamy responded on 28 Jul 2015 9:16 AM

Here is the complete script to get the version of the CRM.Let me know if it doers not work..

function onload_Form() {
var xml = "" +
"<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\"><s:Body><Execute xmlns=\"schemas.microsoft.com/.../Services\" xmlns:i=\"www.w3.org/.../XMLSchema-instance\">" +
"<request i:type=\"b:RetrieveVersionRequest\" xmlns:a=\"schemas.microsoft.com/.../Contracts\" xmlns:b=\"schemas.microsoft.com/.../Contracts\">" +
"<a:Parameters xmlns:c=\"schemas.datacontract.org/.../System.Collections.Generic\"/><a:RequestId i:nil=\"true\"/>" +
"<a:RequestName>RetrieveVersion</a:RequestName></request></Execute></s:Body></s:Envelope>" +
"";
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Accept", "application/xml, text/xml, */*");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
xmlHttpRequest.send(xml);
var resultXml = xmlHttpRequest.responseText;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(resultXml, "text/xml");
}
else // Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(resultXml);
}
var crmVersion = xmlDoc.childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[1].childNodes[0].nodeValue;

alert(crmVersion);
}

Reply