Add a List or Document Library to a Page in SharePoint Online using PowerShell

Requirement: Add a list or document library to a modern page in SharePoint Online.

SharePoint Online: How to add a document library to a page?

In modern SharePoint Online pages, we can add a list or document library to show its contents on the page. In this blog post, we will cover the steps on how to add a list to a page in SharePoint Online. We’ll also see how to add a document library to a page in SharePoint Online using PowerShell.

To add a document library to a page in SharePoint Online, follow these steps:

  • Login to your SharePoint Online site, edit an existing page or create a new one.
  • Hover over the mouse to the page area and click on the “+” icon to add a web part to the pagesharepoint online how to add a list or document library to a page
  • Select either Document Library or list from the available web parts.add list to a modern page in sharepoint online
  • Now, you can pick the desired document library you want to show on your page.
  • You can also click on edit web part to further customize properties of the document library such as folder to show, list view, size, etc. Once you’re done, click Apply.
  • Click on Publish at the top right of your page to make the changes live.add document library to a page in sharepoint online

To add a list to a page in SharePoint Online, you can use PowerShell too.

PnP PowerShell to Add a Document Library or List to the Page

We can automate the above steps to add a list to a page in SharePoint Online with PnP PowerShell. This can be helpful if you want to quickly add a library to the page and don’t want to navigate through the web browser interface.

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
$LibraryName ="Branding"

Try {
    #Connect to the site
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the document library
    $Library = Get-PnPList -Identity $LibraryName

    #Get an existing page
    $Page= Get-PnPPage -Identity $PageName

    #Add a section
    Add-PnPPageSection -Page $Page -SectionTemplate OneColumn -Order 1
    
    #Add a document library to page
    Add-PnPPageWebPart -Page $Page -DefaultWebPartType List -Section 1 -Column 1 -WebPartProperties @{isDocumentLibrary="true";selectedListId=$Library.Id}
    $Page.Publish()
    Write-Host "Document Library has been added to the page successfully!"
}
Catch [System.Exception]
{
    Write-Host -f Red "Error:"$_.Exception.Message
}

You can use “selectedFolderPath=”/your-folder-path” in the “WebPartProperties” to display a specific folder from the document library. Set “isDocumentLibrary” to “false” when you want to add a list to the page.

Creating a new modern page in SharePoint Online using PowerShell is in my other article: How to Create a Page in SharePoint Online using PowerShell?

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

One thought on “Add a List or Document Library to a Page in SharePoint Online using PowerShell

  • Have you found a way to force the webpart to adopt the background color of the section? Microsoft allows background colors.

    Reply

Leave a Reply