
I am trying to get response with return=representation but its saying bad header can anyone help on this
1.How to get errors in return=representation
2.How to add header and retrieve GUID for Creation on record i tried with following code
Dim URL As String = String.Empty
Dim response As HttpResponseMessage
Dim mailMessage As New StringBuilder
Try
System.Net.ServicePointManager.Expect100Continue = True
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
authContext = New AuthenticationContext(authority, False)
clientCredential = New ClientCredential(clientId, appKey)
Dim result As AuthenticationResult = Nothing
result = authContext.AcquireToken(serviceUrl, clientCredential)
httpClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", result.AccessToken)
httpClient.DefaultRequestHeaders.Add("Prefer", "odata.includeannotations=OData.Community.Display.V1.FormattedValue")
httpClient.DefaultRequestHeaders.Add("Prefer", "return=representation ")
URL = "https"//test.dynamics.com" & "/api/data/v9.0/accounts?$select=name" 'Giving dummy url (https"//test.dynamics.com )for security reasons in question change your url
Dim createrequest1 As HttpRequestMessage = New HttpRequestMessage(HttpMethod.Post, URL)
createrequest1.Content = New StringContent(InputJson, Encoding.UTF8, "application/json")
response = httpClient.SendAsync(createrequest1).Result
Dim contact1Uri As String = ""
If response.StatusCode = System.Net.HttpStatusCode.NoContent Then
contact1Uri = response.Headers.GetValues("OData-EntityId").FirstOrDefault()
End If
If (ConfigurationManager.AppSettings("IsEmail").ToString().ToUpper() = "ON".ToUpper() Or String.IsNullOrEmpty(contact1Uri)) Then
Dim strEmailSubject As String = "FAILED: Error in Dynamics post method"
mailMessage.Append("Operation attempted: Error in Dynamics post method" & vbCrLf & vbCrLf)
mailMessage.Append("Error date: " & DateTime.Now & vbCrLf & vbCrLf)
mailMessage.Append("File: " & HttpContext.Current.Request.FilePath & vbCrLf & vbCrLf)
mailMessage.Append("Call: " & Reflection.MethodBase.GetCurrentMethod().Name & vbCrLf & vbCrLf)
mailMessage.Append("General error: " & "Post URL :" URL "
Json Input :" InputJson "
Json Response :" response.ReasonPhrase.ToString() "" response.Content.ToString() "")
Utilities.SendEmail(HttpContext.Current.Application.Item("ErrorEmail_From").ToString, HttpContext.Current.Application.Item("ErrorEmail_High"), String.Empty, String.Empty, strEmailSubject, mailMessage.ToString, False, Net.Mail.MailPriority.Normal)
End If
Return contact1Uri Hi,
Why do you need ?$select=name in URl as this is post request, no need to add this.
Please remove this and try.
See below link for sample cod to perform CRUD operation using WEB API C#.
https://github.com/jlattimer/CrmWebApiCSharp/blob/master/CrmWebApiCSharp/Program.cs