12

I am trying to construct the correct URL to return the items in a SharePoint View using the REST api.

Using my browser and the following URL I can return the items in the list.

https://mysharepoint.sharepoint.com/sites/MySite/_api/web/lists/getbytitle('Announcements')/Items

And I can get the view definition using the following URL.

https://mysharepoint.sharepoint.com/sites/MySite/_api/web/lists/getbytitle('Announcements')/Views/getbytitle('Latest News')/

But I cannot figure out what I need to put at the end of that URL to actually get the items that are returned by the the View.

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
user3658298
  • 341
  • 1
  • 5
  • 15

1 Answers1

14

SP.View object does not contain any methods for manipulating list items. But SP.View object contains SP.View.viewQuery property that specifies the query that is used by the list view. That means the following approach could be used for retrieving list items for view:

  • perform the first request to get CAML Query for List View using SP.View.viewQuery property
  • perform the second request to retrieve List Items by specifying CAML Query

How to return list items for a 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); 
         });
}

Usage

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));
});
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • 5
    Thank you for such a detailed answer. As an aside does anyone know why there isn't a method to return the items in a View? It seems very odd that the API expects the developer to construct and execute the query. – user3658298 Nov 12 '14 at 17:00
  • 1
    @Vadim, This is awesome answer. But, is it possible to retrieve only visible fields for that view. Because above code will retrieve so many fields which are not really required. Ideally I don't want to pass the field name as `items[i].Title` . Is there such possibility? – Mihir Sep 12 '17 at 15:24
  • @Mihir, that's doable i believe via `SP.View.viewFields property` which returns `Gets a value that specifies the collection of fields in the list view.` – Vadim Gremyachev Sep 13 '17 at 09:59