REST APi to get all times in specific folder in SharePoint document library

15,389

Solution 1

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));
});

Solution 2

You need to use a CAML query to specify a "filter"

In this CAML query you can use the field : FileDirRef and for value the serverRelativeURL of your folder.

This is an exemple of how to execute CAML query using REST API : Using CAML with SharePoint REST API

Solution 3

You can access files from specific folders using SharePoint 2013 REST API.

END Point :

http://<site url>/_api/web/getfolderbyserverrelativeurl('/<folder name>')/files

This URL will return only files located under (one level beneath only) the specified folder.

References :

  1. https://msdn.microsoft.com/en-us/library/office/dn450841.aspx
  2. https://msdn.microsoft.com/en-us/library/office/dn292553.aspx
  3. SharePoint REST API getFolderByServerRelativeUrl Returns Nothing
Share:
15,389
Laleh
Author by

Laleh

Updated on June 17, 2022

Comments

  • Laleh
    Laleh almost 2 years

    I would like to get items in specific folder inside SharePoint document library called "Pages" with REST API

    I used below rest API which I can get all items in document library
    https://spsite/_api/web/lists/getByTitle('Pages')/items

    But I have not found the REST api which I can get all times in specific folder inside SharePoint document library

  • Laleh
    Laleh about 7 years
    WOW Thanks Lot , you saved me a lot of time , I tried the first option and it works like grand:) I also can get taxonomy single select value label , which is not possible to get it by _api/web/lists/getByTitle('list name')/items" , but surprisingly now I am not able to get multiple select Taxonomy fields , I get null value by this REST URL ,do you have any idea about it?
  • Laleh
    Laleh about 7 years
    I have another question , I would need to filter as well , I used "_api/web/GetFolderByServerRelativeUrl(<serverrelativefolder‌​url>)/files?$expand=‌​ListItemAllField$fil‌​ter=ACT eq '23' , the "ACT" is columns name which has returned under ListItemAllField , but I get Error message "Field or property \"ACT\" does not exist." , is there any way to filter this?