REST APi to get all times in specific folder in SharePoint document library
There are at least two options available to return items from a specific folder:
1) Using /_api/web/getfolderbyserverrelativeurl('<serverrelativefolderurl>')
endpoint
The following example returns all the files along with associated list items from a specific folder:
/_api/web/getfolderbyserverrelativeurl('<serverrelativefolderurl>')/files?$expand=ListItemAllFields
2) using FolderServerRelativeUrl
property of CAML query
function getListItems(webUrl,listTitle, queryText,folderUrl)
{
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,
"FolderServerRelativeUrl": folderUrl
}
};
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
getListItems(_spPageContextInfo.webAbsoluteUrl,'Pages', '', '/Pages/Archive')
.then(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));
});