Skip to content

Instantly share code, notes, and snippets.

@emregulcan
Created May 2, 2019 22:44
Show Gist options
  • Save emregulcan/4f0d07e070a40ac9d79f283624251a25 to your computer and use it in GitHub Desktop.
Save emregulcan/4f0d07e070a40ac9d79f283624251a25 to your computer and use it in GitHub Desktop.
Dynamics 365 CE (CRM) Web API update existing data
public void Update(string entityLogicalName, Guid id, object updateContent)
{
string apiBaseUrl = $"{_d365Url}/api/data/v9.1";
string apiFullUrl = $"{apiBaseUrl}/{entityLogicalName}s({id.ToString()})";
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), apiFullUrl);
request.Content = new StringContent(JsonConvert.SerializeObject(updateContent), Encoding.UTF8, "application/json");
var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
var content = response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new CrmHttpResponseException(response.Content);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment