1

When we use REST we get output as xml how do we parse this via JSOM and fetch the values?

Also, please provide some information on how to use this in SSOM instead of JSOM?

2
  • Are you asking how to make a REST call using JSOM?
    – wjervis
    Jul 16, 2014 at 12:52
  • I am asking 2 things: 1. when making REST call via JSOM and 2. when making REST calls via SSOM, how do we make use of the result returned?
    – variable
    Jul 17, 2014 at 4:34

1 Answer 1

2

The default format for SharePoint REST endpoint is application/atom+xml. Instead of manipulating results returned in Xml format you could specify JSON format.

Use accept header to specify the format for response data from the server. To get the results in JSON format, include an Accept header set to application/json;odata=verbose.

Examples

The code in the following example shows you how to request a JSON representation of all of the lists in a site by using

JavaScript:

$.ajax({
        url: siteUrl + "/_api/web/lists",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
           console.log(JSON.stringify(data.d.results));
        },
        error: function (data) {
           console.log(JSON.stringify(data));
        }
});

C#

var request = (HttpWebRequest)HttpWebRequest.Create(siteUrl + "/_api/web/lists");
request.Method = "GET";
request.Accept = "application/json;odata=verbose";
request.Headers.Add("Authorization", "Bearer " + accessToken);
var response = (HttpWebResponse)request.GetResponse();
2
  • HI Vadim, I want to know the way in which we can access the result in c# and in javascript. You answer is related to c#. But suppose we use REST in javascript. Then how to parse result?
    – variable
    Jul 23, 2014 at 6:22
  • In JavaScript environment it returns JSON object. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss. See the JavaScript example in the answer, for example, to retrieve the first list title: var title = data.d.results[0].Title: Jul 23, 2014 at 6:50

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.