GitHub Icon Image

Add a document library web part to a page (and only show a specific folder)

Summary

A customer had the requirement to create a page for each of their 86 folders in a document library so they could add more information on those topics. That meant creating 86 pages, each with a document library web part on it that showed a specific folder.

Example Screenshot

The sample creating the page, adding the web parts and includes repeating this for all 86 folders. There is probably a really nice way to, in code, get all folders from the document library and loop through them. So I exported the document library to Excel and copied the folder names. I added some quotes and a comma (in an Excel formula using =CHAR(34) & A2 & CHAR(34) &”,”) and added an array to store these.

  • PnP PowerShell
  • CLI for Microsoft 365 with PowerShell
Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/Yoursite/ -UseWebLogin
$ray = "folder1",
       "folder2",
       "folder3"

foreach ($name in $ray) {

    #create page
    Add-PnPClientSidePage -Name $name -LayoutType Article -HeaderLayoutType NoImage -CommentsEnabled:$false
    
    #add sections
    Add-PnPClientSidePageSection -Page $name -SectionTemplate TwoColumn -Order 1
    
    #add text webpart
    Add-PnPClientSideText -Page $name -Section 1 -Column 1 -Text " "
    
    #add doclib
    Add-PnPClientSideWebPart -Page $name -DefaultWebPartType List -Section 1 -Column 2 -WebPartProperties @{isDocumentLibrary="true";selectedListId="1fa1fb45-e53b-4ea1-9325-ddca7afe986e";selectedFolderPath="/$name";hideCommandBar="false"}
    $page = Get-PnPClientSidePage -Identity $name
    $page.Publish()
}

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell


$site = "https://yourtenant.sharepoint.com/sites/Yoursite/"

$m365Status = m365 status
if ($m365Status -match "Logged Out") {
    m365 login
}

$ray = "folder1",
       "folder2",
       "folder3"

foreach ($name in $ray) {

    #create page
    $fileName = "$name.aspx"
    m365 spo page add --name $fileName --title $name --webUrl $site
    
    #add sections
    m365 spo page section add --name $fileName --webUrl $site --sectionTemplate TwoColumn --order 1
    
    #add text webpart
    m365 spo page text add --webUrl $site --pageName $fileName --text $name --section 1 --column 1
    
    #add doclib
    $webpartProperties = '{\"selectedListId\":\"DC4B61E0-01BE-4A87-B8E1-B9AEF4E34153\",\"selectedFolderPath\":\"' + $name + '\",\"hideCommandBar\":\"false\"}'
    m365 spo page clientsidewebpart add --webUrl $site --pageName $fileName --standardWebPart List --section 1 --column 1 --webPartProperties $webpartProperties
    m365 spo page set --name $fileName --webUrl $site --publish
}

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Source Credit

Sample first appeared on Use PnP Powershell to add a document library webpart to a page (and only show a specific folder) | Tech Community

Contributors

Author(s)
Marijn Somers
Adam Wójcik

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI