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.
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.
- var g;
- $.ajax({
- url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/Lists/GetByTitle('Training Material')/Items",
- type: 'GET',
- dataType: "json",
- headers: {
- "Accept": "application/json;odata=verbose",
- "content-type": "application/json; odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val()
- },
- success: function (data) {
- for (var i = 0; i < data.d.results.length; i++) {
- if (data.d.results[i].FileSystemObjectType != 1) {
- }
- }
- },
- error: function (request, error) {
- console.log(JSON.stringify(request));
- }
- });

ramesh vaghelaPosted Apr 13, 2018, 8:26 AM
Nice article. . thank you for sharing this information
Piyush AgarwalPosted Mar 8, 2018, 7:09 AM
Thanks Hiren, I tried this code to get all the pages in my pages library but could not get them. Is there something to be changes to get all the pages in different folders in a library???