Get All Files From All The Folders Of SharePoint Document Library

In this blog, I will share the code snippets for retrieving all the files from SharePoint document library using REST API.

We can use REST API to communicate and manipulate SharePoint data, for instance, if we have some SharePoint list and if we want to retrieve all the list items from the list. The same thing applies to SharePoint document library. But, retrieving documents from a library is not as easy as from a SharePoint list because, in SharePoint document library, there may be a nested folder structure.

There are a couple of blogs on how to retrieve the documents from SharePoint Document Library by using server/folder relative path but in this blog, I will show you a way to get all the files from library irrespective of the folder structure without using a relative path.
 
Note

If the document library has more than 100 files, then we might need to use $top in URL or we can use data.d.__next property to identify the next set of items.
  1. var g;  
  2. $.ajax({  
  3.     url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists/GetByTitle('Training Material')/Items",  
  4.     type: 'GET',  
  5.     dataType: "json",  
  6.     headers: {  
  7.         "Accept""application/json;odata=verbose",  
  8.         "content-type""application/json; odata=verbose",  
  9.         "X-RequestDigest": $("#__REQUESTDIGEST").val()  
  10.     },  
  11.     success: function (data) {  
  12.         for (var i = 0; i < data.d.results.length; i++) {  
  13.             if (data.d.results[i].FileSystemObjectType != 1) {            
  14.             }  
  15.         }  
  16.     },  
  17.     error: function (request, error) {  
  18.         console.log(JSON.stringify(request));  
  19.     }  
  20. });  
The above code will return all the items of document library including folders. So, to exclude folders, we can use FileSystemObjectType property to determind whether the current item is folder or file. For more information on FileSystemObjectType, visit this link.
Ebook Download
View all
Learn
View all