Using Xrm.WebAPI In Dynamics 365/CRM

With Microsoft releasing Dynamics 365 update 9.0, now, we have a new library to implement WebAPI methods using Xrm.WebApi. Instead of writing the complete request, now we can just use direct the CRUD methods from the Web API.

If you are looking for how to write Web API for Dynamics CRM 2016, check out the sample index here

Xrm.WebApi has two properties to use for the Online and the Offline client respectively. In this article, we are going to implement a sample HTML web resource using Xrm.WebApi for the online client.

Now, to create an entity record, we can simply call Xrm.WebApi Create method using the following parameters.
  1. function createAccount() {  
  2.     // collect account data  
  3.     var data = {  
  4.         "name": document.getElementById("txtname").value,  
  5.         "address1_city": document.getElementById("txtcity").value,  
  6.         "telephone1": document.getElementById("txtphone").value,  
  7.         "numberofemployees": document.getElementById("txttotalemployees").value,  
  8.         "websiteurl": document.getElementById("txtwebsite").value  
  9.    
  10.     }  
  11.     // create account record  
  12.     parent.Xrm.WebApi.createRecord("account", data).then(  
  13.         function success(result) {  
  14.             document.getElementById("txtaccountid").value = result.id;  
  15.             alert("Account Created !!");  
  16.         },  
  17.         function(error) {  
  18.             alert(error.message);  
  19.         }  
  20.     );  
  21. }  
 
 
To update the entity record, we can use the following method and pass the updated data.
  1. function updateAccount() {  
  2.     // collect account data  
  3.     var data = {  
  4.         "name": document.getElementById("txtname").value,  
  5.         "address1_city": document.getElementById("txtcity").value,  
  6.         "telephone1": document.getElementById("txtphone").value,  
  7.         "numberofemployees": document.getElementById("txttotalemployees").value,  
  8.         "websiteurl": document.getElementById("txtwebsite").value  
  9.    
  10.     }  
  11.     //get account id  
  12.     var accountId = document.getElementById("txtaccountid").value;  
  13.    
  14.     // update account record  
  15.     parent.Xrm.WebApi.updateRecord("account", accountId, data).then(  
  16.         function success(result) {  
  17.             alert("Account Record Updated");  
  18.    
  19.         },  
  20.         function(error) {  
  21.             alert(error.message);  
  22.    
  23.         }  
  24.     );  
  25.    
  26. }  
 
 
And, to delete, we can simply pass the entity and record it like following.
  1. function deleteAccount() {  
  2.      var accountId = document.getElementById("txtaccountid").value;  
  3.      parent.Xrm.WebApi.deleteRecord("account", accountId).then(  
  4.          function success(result) {  
  5.              alert("Account deleted");  
  6.    
  7.          },  
  8.          function(error) {  
  9.              alert(error.message);  
  10.    
  11.          }  
  12.      );  
  13.  }  


You can download the complete code from GitHub here. Stay tuned for more Dynamics 365 update 9.0 features !!!


Similar Articles
HIMBAP
We are expert in Microsoft Power Platform.