11

by using rest api

I get items like this

http://mydomain/_api/web/lists/GetByTitle('MyList')/Items

I get the view like this and naturally in it I have the GUID

http://mydomain/_api/web/lists/GetByTitle('MyList')/views/getbytitle('MyView')

Now how can I get all items under this certain view

3 Answers 3

4

Using REST you can't get items from a view. For that you have two options

  1. Get the view fields and form a query
  2. Create a CAML query and use it to get the items.

https://social.msdn.microsoft.com/forums/sharepoint/en-US/a5815727-925b-4ac5-8a46-b0979a910ebb/query-listitems-by-view-through-rest-api

30

The following example demonstrates how to retrieve list items for a View using SharePoint REST:

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 executeJson(url,"POST",null,queryPayload);
}


function getListViewItems(webUrl,listTitle,viewTitle)
{
     var url = webUrl + "/_api/web/lists/getByTitle('" + listTitle + "')/Views/getbytitle('" + viewTitle + "')/ViewQuery";
     return executeJson(url).then(
         function(data){         
             var viewQuery = data.d.ViewQuery;
             return getListItems(webUrl,listTitle,viewQuery); 
         });
}

where

function executeJson(url,method,headers,payload) 
{
    method = method || 'GET';
    headers = headers || {};
    headers["Accept"] = "application/json;odata=verbose";
    if(method == "POST") {
        headers["X-RequestDigest"] = $("#__REQUESTDIGEST").val();
    }      
    var ajaxOptions = 
    {       
       url: url,   
       type: method,  
       contentType: "application/json;odata=verbose",
       headers: headers
    };
    if (typeof payload != 'undefined') {
      ajaxOptions.data = JSON.stringify(payload);
    }  
    return $.ajax(ajaxOptions);
}

Usage

getListViewItems(_spPageContextInfo.webAbsoluteUrl,'Tasks','All Tasks')
.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));
});
4
  • 5
    Thax Vadim Gremyachev, your code is solid :) Commented Jul 30, 2015 at 7:39
  • 1
    Really nice code! I'm using it to format some view data with D3. thanks, Stephan Onisick Commented Mar 30, 2017 at 15:08
  • @Vadim, but in this you are explicitly writing column name as items[i].Title. But is it possible to only get selected fields in that view? Commented Jul 23, 2018 at 17:21
  • 1
    Hello Vadim, I am getting 403 forbidden error for above code? Is there any solution for that? Commented May 31, 2019 at 8:51
1

I usually use this approach when querying a list:

_api/web/lists(guid'D38E6516-FB4F-4FCF-9E29-4FEC9CE06D2B')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query><Where><Eq><FieldRef Name='Your field here'><Value Type='The field type'>The value you want to look for</Value></Eq></Where></Query><ViewFields><FieldRef Name='Id' /></ViewFields></View>"}

The guid being the guid of the list (This saves you some headache if the list name ever changes).

This needs to be called as a POST call, not a GET call (Like your normal browser request).

Also when constructing the CAML part you can help yourself a lot by using a CAML builder like U2U or similar.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.