Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

return=representation error Dynamics WEB API

Posted on by 5

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

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: return=representation error Dynamics WEB API

    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

    If found helpful, Please mark my answer verified.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans