In our earlier articles, we discussed how to write create, retrieve and retrievemultiple requests, today we are going to discuss about writing update request using Web API for Dynamics CRM 2016.
To update data, we have the following two http methods:
PATCH
This method is used to update multiple property of particular entity type. To update multiple properties, we can create object and define properties that we want to update like below, let’s say we want to update account record:
- var account = {};
- account["address1_city"]="Delhi";
- account["address1_country"]="India";
- account["address1_street1"]="ABC Tower";
After that we can pass it to record URI like the following,
- [Organization URL]+"/api/data/v8.0/accounts(Record GUID)"
After that we can pass it to record URI like the following,
- function updateAccount(Id) {
- var serverURL = Xrm.Page.context.getClientUrl();
-
- var account = {};
- account["address1_city"] = "Delhi";
- account["address1_country"] = "India";
- account["address1_street1"] = "ABC Tower";
-
- var req = new XMLHttpRequest();
- req.open("PATCH", serverURL + "/api/data/v8.0/accounts(" + Id + ")", 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 ) {
- req.onreadystatechange = null;
- if (this.status == 204)
- alert("Account is updated")
- } else {
- var error = JSON.parse(this.response).error;
- alert(error.message);
- }
- }
- };
- req.send(JSON.stringify(account));
- }
PUT
This method is used to update single property of particular entity type. We need to pass the property name with entity URI itself and in object we can pass value of the property like the following,
- [Organization URL]+"/api/data/v8.0/accounts(Record GUID) /propertyname"
-
- Var account={"value":"value of the property"};
So the complete code will be like below:
- function updateSingleProperty(Id) {
-
- var serverURL = Xrm.Page.context.getClientUrl();
-
- var account = {
- "value": "1234"
- };
-
- var req = new XMLHttpRequest();
- req.open("PUT", serverURL + "/api/data/v8.0/accounts(" + Id + ")/accountnumber", 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 ) {
- req.onreadystatechange = null;
- if (this.status == 204) {
- alert("Account is updated")
- } else {
- var error = JSON.parse(this.response).error;
- alert(error.message);
- }
- }
- };
- req.send(JSON.stringify(account));}
And in case if we need to update single-valued navigation (lookup), we can do it like following:
- var account = { "[email protected]":"/contacts(51366C53-45AE-E511-80DF-3863BB341BF0)"};
And need to pass URI without property name like the following:
- serverURL+"/api/data/v8.0/accounts(Record GUID)"
Stay tuned for more Web API Samples!