Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Unanswered

WEB API: Update contact - The requested resource does not support http method 'PATCH'.

Posted on by 2,664

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: 

The requested resource does not support http method 'PATCH'. 
Do you have any idea? 
thanks
Alessandro

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,558 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,645 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans