I'm new to REST API in SharePoint. I was required to query some items from given URL of Calendar type list. But I know how to query calendar list items (http://server/_api/web/lists/getbytitle('Calendar')/items?$select=Title). But I don't how to do it for given URL (http://server/Lists/Calendar/MyItems.aspx). Please help me how the REST URL should be to query items from http://server/Lists/Calendar/MyItems.aspx.
1 Answer
The page represents a list view. For example, the URL http://server/Lists/Calendar/MyItems.aspx
in your case may represent a view named "My Items". You can go into the view and check what filters are applied to the view. And then based on that you can call the REST api to get the items using $filter
operator. Example:
http://server/_api/web/lists/getbytitle('Calendar')/items?$select=Title$filter=COLUMN1 eq 'VALUE'
If there are more than one filters, you can use operators like (and/or).
If the filter is complex, you can create a CAML query of the view and use it to get items in the view.
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"
}
});
}
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));
});
Source: https://stackoverflow.com/questions/26884647/using-rest-to-fetch-sharepoint-view-items
-
Thanks for the explanation. Seems good. But I get undefined for var items;... any idea? Mar 12, 2015 at 6:36
-
1@SivakumarPiratheeban, I see that you created a separate question for this issue here sharepoint.stackexchange.com/questions/134939/…. This is good as question gets more attention instead of posting it in comments. I have replied on that thread. Mar 12, 2015 at 8:30