Create a Modern Page in SharePoint Online using PowerShell
Requirement: Create Modern Page in SharePoint Online using PowerShell.
How to Create a Modern Page in SharePoint Online?
- Log in to your SharePoint Online site >> Click on “Settings” gear >> Click on “Site contents” from the menu.
- Now, open the “Site Pages” library on the site contents page >> Click on “New” >> Site Page. You can also use the New >> Page from the toolbar on the home page.
- Select the template and click on the “Create page” button at the bottom of the screen. This opens a new page in edit mode. Provide a name, set the content of the page by adding text, images, videos, etc.
- Click on Publish to create the page.
While creating a page in SharePoint is simple and straightforward, PowerShell is a powerful way to automate the process of creating pages and increase productivity.
SharePoint Online: Create Modern Page using PowerShell
This PowerShell script creates a modern page in a given communication site and adds the News web part and document library.
#Config Variable
$SiteURL = "https://crescent.sharepoint.com/sites/Intranet"
#Connect to PpP Online
Connect-PnPOnline -Url $SiteURL -Interactive # -Credentials (Get-Credential)
#Create new page
$Page = Add-PnPPage -Name "News" -LayoutType Article
#Set Page properties
Set-PnPPage -Identity $Page -Title "News" -CommentsEnabled:$False -HeaderType Default
#Add Section to the Page
Add-PnPPageSection -Page $Page -SectionTemplate OneColumn
#Add Text to Page
Add-PnPPageTextPart -Page $Page -Text "Welcome To News Portal" -Section 1 -Column 1
#Add News web part to the section
Add-PnPPageWebPart -Page $Page -DefaultWebPartType News -Section 1 -Column 1
#Add List to Page
Add-PnPPageWebPart -Page $Page -DefaultWebPartType List -Section 1 -Column 1 -WebPartProperties @{ selectedListId = "21b99d39-834f-4991-b5f9-bd095fa0633c"}
#Publish the page
$Page.Publish()
This creates a News page in full width as below (which is presently not possible through UI!):
In conclusion, creating a modern page in SharePoint Online using PowerShell is a powerful way to automate the process of creating pages and increase productivity can save you a lot of time and effort. By utilizing the PnP PowerShell Add-PnPPage cmdlet, you can easily create modern pages with just a few lines of code. Additionally, once the modern page is created, it can be easily customized and tailored to fit the specific needs of your organization.
You can also create a page from saved page templates: How to Create a new page from Template in SharePoint Online?