Created
May 2, 2019 22:44
-
-
Save emregulcan/4f0d07e070a40ac9d79f283624251a25 to your computer and use it in GitHub Desktop.
Dynamics 365 CE (CRM) Web API update existing data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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