Using Web API Function in CRM 2016 Part 1

If you are developer, you should be familiar with functions. It is a reusable piece of code which can be used to perform different operation based on their definition. CRM 2016 introduced Web API function which can be used to perform different CRM operations. Following are the two functions available in Web API:

Function
Query Function

In this article we will be discussing standard function. Web API function is used to retrieve information from CRM, every function basically represents one request in IOrganization service which can return entity collection or complex type. These functions can be generic or can be associated with specific entity, if function is related to specific entity it is known as bound function and if they are generic, not associated with any entity they are known as unbound function. We can get complete list of function reference from here.

We can call function by appending function name with parameters like below:
serverURL + “/api/data/v8.0/”+Functionname(parameter1,parameter2,..n)

Following is the complete code for using RetrieveVersion function, which can be used to get current CRM version:

function GetCRMVersion() {
    //get CRM URL
    var serverURL = Xrm.Page.context.getClientUrl();

    //write request for function  
    var req = new XMLHttpRequest();
    req.open("GET", serverURL + "/api/data/v8.0/RetrieveVersion()", true);
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */ ) {
            req.onreadystatechange = null;
            if (this.status == 200)//check response is OK {
                var data = JSON.parse(this.response);
                alert("You are using Microsoft Dynamics CRM "+data.Version);
            } else {
                var error = JSON.parse(this.response).error;
                alert(error.message);
            }
        }
    };
    req.send();
}

Once we will use execute this code we will get current CRM version, alert like below:
currentCRMversion

Stay tuned for more Dynamics CRM updates!!

One thought on “Using Web API Function in CRM 2016 Part 1

  1. Pingback: Using Web API Function in CRM 2016 Part 2 | HIMBAP

Leave a Reply

Your email address will not be published. Required fields are marked *