Hi,
I try to use the web api to create a console app where I can update contacts.
I created a method to update contact:
UserDto userupd = new UserDto();
userupd.firstname = "Test11231";
userupd.lastname = "Test12112";
userupd.contactid = Guid.Parse("891330a0-274b-ec11-8c62-000d3ada7f0d");
string body = Newtonsoft.Json.JsonConvert.SerializeObject(userupd);
var userdetailAnswer = CrmRequest2(
new HttpMethod("PATCH"),
String.Format("{0}/{1}/contacts", "">xxxxxx.api.crm4.dynamics.com", "api/data/v9.2/"),
body)
.Result.Content.ReadAsStringAsync();
where CrmRequest2 is:
public static async Task<HttpResponseMessage> CrmRequest2(HttpMethod httpMethod, string requestUri, string body = null)
{
// Acquiring Access Token
var accessToken = await AccessTokenGenerator();
var client = new HttpClient();
var message = new HttpRequestMessage(httpMethod, requestUri);
// OData related headers
message.Headers.Add("OData-MaxVersion", "4.0");
message.Headers.Add("OData-Version", "4.0");
message.Headers.Add("Prefer", "odata.include-annotations=\"*\"");
message.Headers.Add("Accept", "application/json");
// Passing AccessToken in Authentication header
message.Headers.Add("Authorization", $"Bearer {accessToken}");
// Adding body content in HTTP request
if (body != null)
message.Content = new StringContent(body, UnicodeEncoding.UTF8, "application/json");
HttpResponseMessage httpResponseResult =
client.SendAsync(message, HttpCompletionOption.ResponseContentRead).Result;
if (httpResponseResult != null)
{
IEnumerable<string> uri = null;
httpResponseResult.Headers.TryGetValues("OData-EntityId", out uri);
if (uri != null)
{
string entityId = uri.FirstOrDefault();
}
}
return await client.SendAsync(message);
}
When I execute the code, I have the error: