Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 342
Laleh Avatar asked Feb 28 '17 11:02

Laleh


People also ask

How do I search all folders in SharePoint?

The easiest way to search for documents in SharePoint Online is to use the search bar at the top of your site. By typing a phrase up here, SharePoint will show you a selection of files and folders that are related to your search query.

How do you mass or bulk check in multiple files with SharePoint?

To check in multiple files at onceIn the document library, select all the files you want to check in by clicking on the check mark to the left of the document's icon. Select Check in.

How do I add a folder to a SharePoint library using REST API?

Now, we will see how to create a folder inside a document library using the JavaScript object model(jsom) in SharePoint Online Office 365. Here we will take a textbox and a button where a user can give a name for the folder and click on Create Folder button which will create the folder inside the document library.


2 Answers

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));
});
like image 114
Vadim Gremyachev Avatar answered Nov 03 '22 00:11

Vadim Gremyachev


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

like image 37
Nico Avatar answered Nov 02 '22 22:11

Nico