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 /* complete */ ) {
- req.onreadystatechange = null;
- if (this.status == 204)//OK {
- 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); //we want to update accountnumber field
- 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 == 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!

RAJEEV KUMARPosted Apr 7, 2016, 6:44 AM
Unable to set Guid value in activity party from field. if I hard cord the guid then It is working fine.var UserID = Xrm.Page.context.getUserId(); alert(UserID); // not working sender["[email protected]"] = "/systemusers(UserID)"; not working. //sender["[email protected]"] = UserID;
Gowtham RajamanickamPosted Jan 6, 2016, 1:21 AM
good article..
Santhakumar MunuswamyPosted Dec 31, 2015, 10:37 AM
Thanks for nice article