How to parse the output returned from REST (/_api/)?
-
29-09-2020 - |
Question
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?
Solution
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();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange