1

I am successfully connected with SharePoint Online and successfully Updated the List using Rest Api.

Now I am trying to Get item details using ItemID using the following C# Code:

try
{
    Uri uri = new Uri(MasterData.SiteURL);

    using (var client = new SPHttpClient(uri, MasterData.UserID, MasterData.PWD))
    {
        var listTitle = "Overdue POs List";
        var itemPayload = new { __metadata = new { type = "SP.Data.Overdue_x0020_POs_x0020_ListListItem" } };
        var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items({2})", uri, listTitle, ItemID);


        var keyValues = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("Authorization", "Bearer "+accessToken)
                };

        HttpContent content = new FormUrlEncodedContent(keyValues);

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);

        var response = httpClient.GetAsync(endpointUrl).Result;
        var result = response.Content.ReadAsStringAsync().Result;
        var status = (JsonConvert.DeserializeObject<OverduePOs>(result.ToString())).PO_x0020_Status;
        return status;
    }
}
catch (HttpRequestException ex)
{
    return "";
}

The response is successfully coming in XML Format but I am unable to Desearilize that in JSON:

Response:

enter image description here

Exception:

Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: <. Path '', line 0, position 0.'

can anyone Help to parse results in JSON?

2
  • Have you tried putting httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); // Specify JSON format for your request and see if it returns in JSON format?
    – Pradip R.
    Aug 8, 2023 at 12:15
  • 1
    Oh Yesss, it resolved my issue. thank you soo much!!!! please add this as Answer Aug 8, 2023 at 12:19

1 Answer 1

1

So, by incorporating the provided code snippet into your request body, your concern will be resolved:

httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); // Indicate JSON format

Essentially, this instruction informs the HTTP request to deliver the response in JSON format.

here is your updated code block:

try
{
    Uri uri = new Uri(MasterData.SiteURL);

    using (var client = new SPHttpClient(uri, MasterData.UserID, MasterData.PWD))
    {
        var listTitle = "Overdue POs List";
        var endpointUrl = string.Format("{0}/_api/web/lists/getbytitle('{1}')/items({2})", uri, listTitle, ItemID);

        var httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
        httpClient.DefaultRequestHeaders.Add("Accept", "application/json"); // Specify JSON format

        var response = httpClient.GetAsync(endpointUrl).Result;
        if (response.IsSuccessStatusCode)
        {
            var result = response.Content.ReadAsStringAsync().Result;
            var overduePOs = JsonConvert.DeserializeObject<OverduePOs>(result);
            var status = overduePOs.PO_x0020_Status;
            return status;
        }
        else
        {
            return "";
        }
    }
}
catch (HttpRequestException ex)
{
    return "";
}
1
  • 1
    Thank you so much Aug 16, 2023 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.