“error”: {
“code”: “”,
“message”: “The requested resource does not support http method ‘PATCH’.”
}
public async Task SetParameterAsync(Account accountSelected)
{
try
{
// Tries to get an access token for the current user with the default set of permissions.
var tokenResult = await TokenProvider.RequestAccessToken();
// If the token request was successful
if (tokenResult.TryGetToken(out var token))
{
// Creates an HttpClient based on the named definition found in Program.Main
var client = ClientFactory.CreateClient(“DataverseClient”);
// Prepare the request to update the account
var request = new HttpRequestMessage()
{
Method = HttpMethod.Patch,
//RequestUri = new Uri($"{client.BaseAddress}accounts({id})?$select=name,telephone1,address1_city&$expand=primarycontactid($select=fullname,emailaddress1)")
RequestUri = new Uri($"{client.BaseAddress}accounts({accountId})")
};
// Add the access token
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.Value);
// Include updated account data in the request body
request.Content = JsonContent.Create(accountSelected, typeof(Account));
//Specify a JSON result is expected
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Headers.Add("Prefer", "return=representation");
// Send the request
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
// If successful, you might want to do something (optional)
// For example, read the updated account information from the response
accountSelected = await response.Content.ReadFromJsonAsync<Account>();
return accountSelected;
}
else
{
//Parse the JSON returned into a strongly typed Error
var error = await response.Content.ReadFromJsonAsync<Error>();
error.statuscode = (int)response.StatusCode;
error.reason = response.ReasonPhrase;
//Display a message to the user
message = "An error occurred.";
//Log the details so they can be seen in the browser console
//logger.LogError($"{error.detail.message}");
return null;
}
}
else
{
// Notify user that the token request was not successful
Console.WriteLine("Problème de modification.");
return null;
}
}
catch (Exception ex)
{
Logger.LogError($"An error occurred while updating account: {ex.Message}");
return null;
}
}
Sayyora Pardaeva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.