0

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.

2
  • 1
    Check this to loop through all subsites of a site collection. Jan 21, 2021 at 3:58
  • Thank you that works perfectly - is there code for looping through all document libraries of a site? Jan 21, 2021 at 5:22

2 Answers 2

1
   //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");
  }
2

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");
1
  • Yep this worked thanks. Jan 22, 2021 at 1:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.