Get Api Version Dynamically For WebApi Requests

In this post I will quickly cover the requirement of dynamically getting API version of dynamics CRM in JavaScript rather then hard coding it , so that in next releases your JavaScript doesn’t break. I am referring to this:

1

For Dynamics CRM version 8.2 or below, do this:

var apiVersion = Xrm.Page.context.getVersion();

For Dynamics CRM version 9 or above, do this:

var apiVersion = Xrm.Utility.getGlobalContext().getVersion();

2

It will give you an expanded version which includes minor and major versions. Now, all you have to do is crop the number to get only initial numbers such as 9.1 in this case:

var shortVersion= apiVersion.substring(3, myStr.indexOf(“.”) -1);

then finally use it in your request as:

    req.open("GET",Xrm.Utility.getGlobalContext().getClientUrl()+ "/api/data/"+shortVersion+"/systemusers

Hope this helps!

Advertisement

4 thoughts on “Get Api Version Dynamically For WebApi Requests

  1. Pingback: Get Api Version Dynamically For WebApi Requests - Microsoft Dynamics CRM Community

  2. Do you have any type of tips for writing articles?
    That’s where I constantly battle as well as I simply end up staring vacant
    screen for long period of time.

    Like

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.