SharePoint Online: Add Web Part to a Modern Page using PowerShell
Requirement: Add a web part to the modern page in SharePoint Online using PowerShell.
How to Add a Web Part to a Modern SharePoint Online Page?
Web parts are pre-built or custom-developed pieces of functionality that can be added to a page to display content, interact with users, or perform a specific task. SharePoint Online allows you to customize the layout and functionality of your site by adding web parts to pages. Adding a Web Part to a Page in SharePoint Online is a simple process. This blog post will show you how to add a Web Part to a modern Page in SharePoint Online. We will also show you how to use PowerShell to add a web part to a modern page in SharePoint Online.
To add a web part to the SharePoint Online page, follow these steps:
- Login to your SharePoint Online site in the web browser >> Create a new page or edit an existing page.
- Hover over the mouse to the body area and click on the “+” icon to add a web part to the page.
- From the available web parts, select the desired web part to add it to the modern page.
- Now, You can click on the web part to set its properties, such as “Text” for the text web part.
- You can also click on the edit web part to further customize properties of the web part such as text styles, color, links, etc.
- Once you’re done, Click on Publish in the top-right corner of your page to make the changes live.
PowerShell to Add a Web Part to a Modern Page
Thinking about updating modern site pages and adding web parts to them? To add a web part to a modern page, You can use PowerShell as well! In this blog post, we’ll show you how to add a Web Part to a page in SharePoint Online using PowerShell. This is a handy technique to know if you want to make changes to multiple pages at once or if you wish to add content to a page programmatically and want to automate the process.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
Try {
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the page
$Page= Get-PnPPage -Identity $PageName
#add text web part to the modern page
Add-PnPPageTextPart -Page $Page -Text "Welcome to the Brand Center!"
$Page.Publish()
Write-Host "Web Part has been added to the page successfully!" -f Green
}
Catch [System.Exception]
{
Write-Host -f Red "Error:"$_.Exception.Message
}
You can position the web part to a specific section of the page by providing section, column, and order values. E.g.
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the page
$Page= Get-PnPPage -Identity $PageName
$WebPartText = "<h2>Welcome to Branding Portal</h2>What colour palette you use, what type of imagery to pick, `
what fonts go well together, how to capture that unique tone of voice. There’s so much to devise, plan out, `
and execute, and it can be frustrating and use up time that you just don’t have. So, we thought we’d make it a bit easier for you."
#Add Text to Section 1 column 1 and in 2nd order
Add-PnPPageTextPart -Page $Page -Text $WebPartText -Order 1 -Section 1 -Column 1
$Page.Publish()
This adds a text web part just below the one already inserted (whose order is 0) into the page.
How to Add Web Parts Like List, Image, People, etc.?
How about adding other web parts such as List, document library, image, People, etc.? Well. Here is how it works:
#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
$PageName ="Branding-Templates.aspx"
$ListName ="Projects"
Try {
#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive
#Get the list to display
$List = Get-PnPList -Identity $ListName
#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 @{selectedListId=$List.Id}
$Page.Publish()
}
Catch [System.Exception]
{
Write-Host -f Red "Error:"$_.Exception.Message
}
You must specify the “DefaultWebPartType” parameter and then “WebPartProperties” accordingly. Similarly, to add the People web part, use the following:
#Set Web Part Properties
$WP = '{"layout":1,"persons":[{"id":"steve@crescent.com","role":""}],"title":"Contacts"}'
#Add web part to page
Add-PnPPageWebPart -Page $Page -DefaultWebPartType People -Section 1 -Column 1 -WebPartProperties $WP
You can also use this same process to add other types of web parts. This is just one example of the many things you can do with PowerShell and SharePoint Online. Here is another post on adding a document library to a modern page in SharePoint Online: How to Add a Document Library to a Page in Sharepoint Online using PowerShell?
Conclusion
In conclusion, adding web parts to a modern SharePoint Online page is a simple process that allows you to customize the layout and functionality of your site. By following the steps outlined in this tutorial, you can easily add new web parts to your pages and create a more dynamic and interactive experience for your users. By using the script provided in this tutorial, you can easily add new web parts to your pages using a script, saving you time and effort when creating content on your site.
How can you edit a Section via PnP to change from one column to two columns so I can add webparts to the second column? This has to be set on over 300 site pages. It appears you can only change via the website when editing the page/Section.
For adding a page properties webpart “PageFields” via PnP how do you set the property to query?
Add-PnPPageWebPart -Page $page -DefaultWebPartType “PageFields”
Is passing in Json properties required which first requires obtaining the list field (in this case Topics)? Then use -webpartproperties to pass in the Json data?
Worked it out.
$field = get-pnpfield -List SitePages -identity “Topics”
$topicfieldID = $field.ID
$topicfieldIDstring = “$topicfieldID”
$pageProperties = @{
title= “Topics”;
selectedFieldIds= @(“$topicfieldIDstring”);
availableFields= @(“”);
};
Add-PnPPageWebPart -Page $pagename -DefaultWebPartType “PageFields” -section 1 -order 5 -column 2 -webpartproperties $pageProperties