跳转至

How to get all items in a view using REST API.note

Source: /Volumes/X9 Pro/ObsNotes/YoudaoYunNotes/默认笔记本/How to get all items in a view using REST API.note.pdf Converted: 2025-12-10 12:19:24


3 Answers

How to get all items in a view using REST API

up vote4down vote

2

favorite

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

rest list-view custom-list

add a comment

shareimprove this question

asked Mar 21 '15 at 15:46

Ergec

1631110

activeoldestvotes

up vote3down voteaccepted

Using REST you can't get items from a view. For

that you have two options

Get the view fields and form a query

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

add a comment

shareimprove this answer

answered Mar 21 '15 at 15:52

Amal Hashim

22.8k51647

up vote15down vote The following example demonstrates how to retrieve

list items for a View using SharePoint REST:

function getListItems(webUrl,listTitle, queryText)

{

var viewXml = '' + queryText +

'';

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.webAbsolute

Url,'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));

});

add a comment

shareimprove this answer

answered Mar 23 '15 at 7:42

Vadim Gremyachev

30.5k33490

Thax Vadim Gremyachev, your code is solid :) –

user44680 Jul 30 '15 at 7:39

Really helped me, thanks ;) –

KGahbicheNov 5

'15 at 17:21

2

2

up vote1down vote

I usually use this approach when querying a list:

_api/web/lists(guid'D38E6516-FB4F-4FCF-9E29-

4FEC9CE06D2B')/GetItems(query=@v1)?@v1=

{"ViewXml":"

<Value

Type='The field type'>The value you want to

look for

"}

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.