Question

Is there any PnPJS code out there, given a site collection URL, loop through and get all files in the site collection (loop through top level, subsites, basically all document libraries) ?

Or even as a start - just given a site collection URL - loop through all subsites?

Unfortunately don't have access to powershell. This is SP 2013.

Was it helpful?

Solution

   //configure sp context for each new site we visit
   configureSP(url) {
    //url comes in relative
    // prod environment needs full url
    // dev environment is just relative url
    if (process.env.NODE_ENV !== "development") {
      url = "http://my.domain.net" + url;
    }

    sp.setup({
      sp: {
        headers: {
          Accept: "application/json;odata=verbose", //set header to get json instead of xml
        },
        baseUrl: url,
      },
    });

    return true;
  }  

 // get doc libraries in currently defined sp web context
 async getDocLibraryInSite() {

     // exclude ootb doc libraries from being found
    let exclusion = new Set([
      "Composed Looks",
      "Master Page Gallery",
      "Site Assets",
      "Style Library",
      "User Information List",
    ]);

    let result = [];
    await sp.web.lists.get().then((data) => {
      console.log("-------Getting Doc Libraries--------------");
      if (data) {
        for (let d of data) {
          if (d.EnableFolderCreation && !exclusion.has(d.Title)) {  
            result.push(d);
          }
        }
      }
    });
    console.log("------------------------------------------------");
    return result;
  }

  async getFiles(folderUrl) {
    await sp.web
      .getFolderByServerRelativeUrl(folderUrl)
      .expand("Folders, Files")
      .get()
      .then((r) => {
        if (r) {
          r.Folders.results.forEach((item) => {
            if (item.Name !== "Forms") { //ignore Edit/DisplayForm folders
              this.getFiles(item.ServerRelativeUrl);
            }
          });
          r.Files.results.forEach((item) => {
            console.log(item.ServerRelativeUrl);
          });
        }
      })
      .catch( (e) => {console.log(e)} );
  }
  

  //given a site collection, get a ll files
  async getFilesForSiteCollection(url) {
    this.configureSP(url);
    let docLibraries = await this.getDocLibraryInSite();

    for (let dl of docLibraries) {
      await this.getFiles(dl.EntityTypeName.replaceAll("_x0020_", " "));
    }
    
    //recursively call for each subsite
    await sp.web.webs.get().then((data) => {
      for (let subsite of data) {
        if (data) {
          this.getFilesForSiteCollection(subsite.ServerRelativeUrl);
        }
      }
    });
  }
  
  //starting point
  componentDidMount() {
    this.getFilesForSiteCollection("/sites/serverRelativeURL");
  }

OTHER TIPS

To loop all subsites:

$pnp.sp.site.rootWeb.webs.get().then(function(data){
    console.log(data)
})

To get all document libraries in the site:

$pnp.sp.site.getDocumentLibraries("http://sp16/sites/michael").then(function(data){
    console.log(data)
})

Get all files in a library:

let getFiles = (folderUrl) => {
    $pnp.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("/sites/michael/Documents");
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top