Created
May 2, 2019 22:42
-
-
Save emregulcan/bae3bfa78a372e476a16756a6bc0733a to your computer and use it in GitHub Desktop.
Dynamics 365 CE (CRM) Web API create 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 string Create(string entityLogicalName, object data) | |
| { | |
| string result = string.Empty; | |
| string apiBaseUrl = $"{_d365Url}/api/data/v9.1"; | |
| string apiFullUrl = $"{apiBaseUrl}/{entityLogicalName}s"; | |
| using (HttpClient httpClient = new HttpClient()) | |
| { | |
| httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); | |
| httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | |
| HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, apiFullUrl); | |
| request.Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json"); | |
| var response = httpClient.SendAsync(request).GetAwaiter().GetResult(); | |
| var content = response.Content.ReadAsStringAsync(); | |
| if (response.IsSuccessStatusCode) | |
| { | |
| if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NoContent) | |
| { | |
| var responseHeaders = response.Headers; | |
| var entityIdHeader = responseHeaders.GetValues("OData-EntityId").FirstOrDefault(); | |
| result = entityIdHeader; | |
| } | |
| } | |
| else | |
| { | |
| throw new CrmHttpResponseException(response.Content); | |
| } | |
| } | |
| return result; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This method returns created records URI like https://organization.crm.dynamics.com/api/data/v9.1/contacts(1a6397b2-206d-e411-a836-01023ac86fa4)