The SP.View object does not contain any methods for managing list items. But the SP.View Object contains the SP.View.viewQuery Property which sets the query that is used in the list view. This means that you can use the following approach to retrieve list items for viewing:
- execute the first query to get a CAML request to view the list using the SP.View.viewQuery property
- run a second query to retrieve list items by specifying a CAML request
How to return list items for list view using REST API using JavaScript
function getJson(url) { return $.ajax({ url: url, type: "GET", contentType: "application/json;odata=verbose", headers: { "Accept": "application/json;odata=verbose" } }); } function getListItems(webUrl,listTitle, queryText) { var viewXml = '<View><Query>' + queryText + '</Query></View>'; var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; var queryPayload = { 'query' : { '__metadata': { 'type': 'SP.CamlQuery' }, 'ViewXml' : viewXml } }; return $.ajax({ url: url, method: "POST", data: JSON.stringify(queryPayload), headers: { "X-RequestDigest": $("#__REQUESTDIGEST").val(), "Accept": "application/json; odata=verbose", "content-type": "application/json; odata=verbose" } }); } function getListItemsForView(webUrl,listTitle,viewTitle) { var viewQueryUrl = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery"; return getJson(viewQueryUrl).then( function(data){ var viewQuery = data.d.ViewQuery; return getListItems(webUrl,listTitle,viewQuery); }); }
Using
getListItemsForView(_spPageContextInfo.webAbsoluteUrl,'Announcements','Latest News') .done(function(data) { var items = data.d.results; for(var i = 0; i < items.length;i++) { console.log(items[i].Title); } }) .fail( function(error){ console.log(JSON.stringify(error)); });
source share