web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How to update record in CRM Dynamics 365 using Web API?

(0) ShareShare
ReportReport
Posted on by 90

We have a web app (ASP.Net) published on Azure and we connected the web app with CRM Dynamics 365 on Azure Active Directory. 

We followed this tutorial https://debajmecrm.com/2016/02/29/dynamics-crm-web-api-external-application/ and we were able to retrieve records from crm using this method: 

{var webRequest = (HttpWebRequest)WebRequest.Create(new Uri("https://XXX.crm.dynamics.com/api/data/v8.0/accounts?$select=name,address1_city&$top=10"));
webRequest.Method = "GET";
// webRequest.ContentLength = 0;
webRequest.Headers.Add("Authorization", String.Format("Bearer {0}", authResult.AccessToken));
webRequest.Headers.Add("OData-MaxVersion", "4.0");
webRequest.Headers.Add("OData-Version", "4.0");
webRequest.ContentType = "application/json; charset = utf-8";
using (var response = webRequest.GetResponse() as System.Net.HttpWebResponse)
{
//Get reader from response stream
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
{
var accounts = new List<Account>();
string responseContent = reader.ReadToEnd();

dynamic dynamicObj = JsonConvert.DeserializeObject(responseContent);

foreach (var data in dynamicObj.value)
{
var account = new Account
{AccountName = data.name.Value,
City = data.city != null ? data.city.Value : string.Empty};

accounts.Add(account);}

Now i want to update record in crm using the same method(HttpWebRequest). How can i do that?

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: How to update record in CRM Dynamics 365 using Web API?

    To retrieve data from an entity you are updating you can compose your PATCH request so that data from the created record will be returned with a status of 200 (OK). To get this result, you must use the return=representation preference in the request headers.

    To control which properties are returned, append the $select query option to the URL to the entity set. The $expand query option will be ignored if used.

    This example updates an account entity and returns the requested data in the response.

    Request

    HTTPCopy
    PATCH [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000001)?$select=name,creditonhold,address1_latitude,description,revenue,accountcategorycode,createdon HTTP/1.1  
    OData-MaxVersion: 4.0  
    OData-Version: 4.0  
    Accept: application/json  
    Content-Type: application/json; charset=utf-8  
    Prefer: return=representation  
      
    {"name":"Updated Sample Account"}  
    

    Response

    HTTPCopy
    HTTP/1.1 200 OK  
    Content-Type: application/json; odata.metadata=minimal  
    Preference-Applied: return=representation  
    OData-Version: 4.0  
      
    {  
        "@odata.context": "[Organization URI]/api/data/v9.0/$metadata#accounts/$entity",  
        "@odata.etag": "W/\"536537\"",  
        "accountid": "00000000-0000-0000-0000-000000000001",  
        "accountcategorycode": 1,  
        "description": "This is the description of the sample account",  
        "address1_latitude": 47.63958,  
        "creditonhold": false,  
        "name": "Updated Sample Account",  
        "createdon": "2016-09-28T23:14:00Z",  
        "revenue": 5000000.0000,  
        "_transactioncurrencyid_value": "048dddaa-6f7f-e611-80d3-00155db5e0b6"  
    }  
      
    
    I hope this helps!
    Regards,
    Lewis
    Developer
    apps4rent | o365cloudexperts
  • Suggested answer
    LeoAlt Profile Picture
    16,331 Moderator on at
    RE: How to update record in CRM Dynamics 365 using Web API?

    Hi partner,

    About how to use Web Api to update records in D365, you could refer to the following links for formats and samples.

    3438.pastedimage1580991120868v1.png

    https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/web-api-basic-operations-sample#bkmk_section1

    https://stackoverflow.com/questions/55429393/how-to-post-data-in-dynamics-365-using-httpwebrequest

    Best Regards,

    Leo

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at
    RE: How to update record in CRM Dynamics 365 using Web API?

    Hello, 

    Please refer to this sample code found in: http://himbap.com/blog/?p=2043

    var crmUrl="xxx";
    var contactId
    var contact = {};
    contact["description"] = "xxx";
    contact["emailaddress1"] = "xxx";
    
    var req = new XMLHttpRequest();
    req.open("PATCH", crmUrl   "/api/data/v8.0/accounts("   contactId   ")", 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)
                console.log("contact is updated");
        } else {
            var error = JSON.parse(this.response).error;
            console.log(error.message);
        }
    }
    req.send(JSON.stringify(contact));
    

    Additionally, you need to add your access_token in the request header. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
DAnny3211 Profile Picture

DAnny3211 359

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 137 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 77 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans