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:

  1. Login to your SharePoint Online site, edit an existing page or create a new one.
  2. 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
  3. Select either Document Library or list from the available web parts. E.g., Task list, Documents, etc.add list to a modern page in sharepoint online
  4. Now, you can pick the desired document library you want to show on your page.
  5. 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.
  6. 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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time 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

Your email address will not be published. Required fields are marked *