Some developers are already using this method in their code, personally I didn't because I had not the necessity to differentiate between the 8.x endpoints.
The method returns a string containing the full version in the Major.Minor.Build.Revision format, an example is "8.2.1.185".
In the previous CRM versions if we want to get the version from the client we had two ways:
- call the RetrieveVersionRequest message using the SOAP endpoint (example)
- check if a specific CRM function exists (example)
Why we want to know the current CRM version? Because some functions are available only from a specific version, like the one to execute a workflow: the Microsoft.Dynamics.CRM.ExecuteWorkflow introduced with the 8.2 release.
But for the Web API endpoint we need to provide only Major.Minor and not the full version, therefore a transformation is necessary.
The following code maybe will be considered exaggerated by someone but creating a robust piece of code will reduce the necessity to change the script in the future:
function getCurrentVersion(context) { if (context.getVersion == undefined) { return ""; } var versionArray = context.getVersion().split("."); if (versionArray.length < 2) { return ""; } return versionArray[0] + "." + versionArray[1]; }The result from the "8.2.1.125" is "8.2" ready to be inserted inside your Web API url.
Few considerations:
- If the version cannot be retrieved I decided to return an empty string, but you can return null, throw an exception, call another method, set a default value, etc etc...
- the function requires the context, in this way you can use in the form scripts (like an onload event) or inside a WebResource
- I decided to split the string to an array using the dot (.) I think this is the most robust way compared to doing a substring or expecting a second dot in the string