SharePoint / PnP-JS-Core

Code moved to https://github.com/pnp/pnpjs. This repository is archived.
Other
379 stars 231 forks source link

[Question]: Retrieve all files within a document library #813

Closed Shyammi closed 6 years ago

Shyammi commented 6 years ago

Hi,

I have a project requirement where I need to retrieve files from all folders and sub folders within a document library and display it in a tabular format. I could manage to get files from root level using the following query

$pnp.sp.web.folders.getByName(LibraryName).files.get()..then(function(data){ });

Similarly, I managed to retrieve files present within a folder by passing relative URL, but I'm struggling to retrieve all the files present within folders and subfolders of a document library.

Any help is much appreciated!!!

Thanks, Shyam

tavikukko commented 6 years ago

Something like this?


let getFiles = (folderUrl) => {
    sp.web.getFolderByServerRelativeUrl(folderUrl)
        .expand("Folders, Files").get().then(r => {
            r.Folders.forEach(item => {
                getFiles(item.ServerRelativeUrl);
            })
            r.Files.forEach(item => {
                console.log(item.ServerRelativeUrl);
            })
        });
}

getFiles("/Style Library");
Shyammi commented 6 years ago

It worked!!!

Thanks a lot, Tavikukko..