Using Web API Function In CRM 2016 - Part One

If you are developer, you should be familiar with functions. It is a reusable piece of code which can be used to perform different operations based on their definition. CRM 2016 introduced Web API function which can be used to perform different CRM operations. The 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)

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

  1. function GetCRMVersion() {  
  2.     //get CRM URL  
  3.     var serverURL = Xrm.Page.context.getClientUrl();  
  4.     //write request for function    
  5.     var req = new XMLHttpRequest();  
  6.     req.open("GET", serverURL + "/api/data/v8.0/RetrieveVersion()"true);  
  7.     req.setRequestHeader("Accept""application/json");  
  8.     req.setRequestHeader("Content-Type""application/json; charset=utf-8");  
  9.     req.setRequestHeader("OData-MaxVersion""4.0");  
  10.     req.setRequestHeader("OData-Version""4.0");  
  11.     req.onreadystatechange = function() {  
  12.         if (this.readyState == 4 /* complete */ ) {  
  13.             req.onreadystatechange = null;  
  14.             if (this.status == 200)//check response is OK {  
  15.                 var data = JSON.parse(this.response);  
  16.                 alert("You are using Microsoft Dynamics CRM "+data.Version);  
  17.             } else {  
  18.                 var error = JSON.parse(this.response).error;  
  19.                 alert(error.message);  
  20.             }  
  21.         }  
  22.     };  
  23.     req.send();  
  24. }  
Once we will execute this code we will get current CRM version alert like below:

 

Stay tuned for more Dynamics CRM updates!
 
Read more articles on CRM: