Update CRM online 2016 records from custom application using WEB API

Category: dynamics crm development


Gugan A on Thu, 08 Sep 2016 11:15:57

I am using below code to update the crm records from custom html application(using CRM 2016 online web API). We have already registered our custom application using Azure Ad in O365 and the we are able to retrieve the records from CRM successfully in the custom application(Html).

Now  we are trying to update the MS CRM online 2016 records from custom application, application runs successfully without any error but the records are not updated in MS CRM. So while debugging my code i have received some error 

PATCH https://crmurl.crm.dynamics.com/api/data/v8.0/accounts(b2c9cceb-236c-e611-80db-c4346bb59860) (anonymous function) 
XMLHttpRequest cannot load https://crmurl.crm.dynamics.com/api/data/v8.0/accounts(b2c9cceb-236c-e611-80db-c4346bb59860). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:65343' is therefore not allowed access. The response had HTTP status code 401.

here  http://localhost:65343 is my custom application url(same url registered in Azure Ad and i am able to retrieve records from CRM)

Update JS Code:

             var account = {};
               account["name"] = "Test"; 
               account["address1_city"] = "Test City"

              var req= new XMLHttpRequest();
               req.open("PATCH", "https://crmrocks.crm.dynamics.com/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.setRequestHeader("Access-Control-Allow-Origin", "http://localhost:65343/Accounts.html");
//           req.setRequestHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
//           req.setRequestHeader("Access-Control-Allow-Headers", "Content-Type");
               if (req.readyState == 4 /* complete */) {
                   if (req.status == 204) {
                   } else {
                       alert("Not Completed");

Kindly help me to resolve this error.


Gugan A on Thu, 08 Sep 2016 12:05:07

I am able to update the record from custom application using WEB API by adding below line.

req.setRequestHeader("Authorization", "Bearer " + token);

M.Abuelsoud on Mon, 07 Nov 2016 07:09:46

Hi Gugan,

How you got the token?