Create Modern Page and Add Webpart to It Using PnP PowerShell
Softree Technology > Blogs > PnP PowerShell > Create Modern Page and Add Webpart to It Using PnP PowerShell

Create Modern Page and Add Webpart to It Using PnP PowerShell

  • Posted by: Softree Technology

In this blog, we will discuss how we can create a modern site Page programmatically using PnP online. Then we will add a hero Webpart programmatically to it. To perform the procedure, we must follow the below steps.

Step-1: – First, we have to open the windows PowerShell and then run the command “Connect-PnPOnline -Url   providesiteUrl”. Then run ISE as administrator as shown below (Right click on windows PowerShell).

Step-2: – Store site URL in an object siteURL. Then connect PnP online and ask for the credential as shown in the below code.

# Provide URL of the Site over here    $siteURL = https://sharepoint.com/sites/sitename

# If you do not wish to pass credentials hard coded then you can use: -Credentials (Get-Credential).  This will prompt to enter credentials  Connect-PnPOnline -Url $siteURLCredentials (Get-Credential). Provide valid credential for SharePoint site.

Step-3: – Here, we are going to create a modern site page having title “Homepage” by using command “Add-PnPClientSidePage”.

Step-4: – We will add a section to the page by using the command Add-PnPClientSidePageSection.

Step-5: – Now in the last step, we will add a hero webpart to the page which we have created. Here, for this we require the below command Add-PnPClientSideWebPart. Now, please follow the below code snippet present in the image for the complete coding.

# Provide URL of the Site over here
$siteURL = "https://sharepoint.sharepoint.com/sites/Site"
# If you do not wish to pass credentials hard coded then you can use: -Credentials (Get-Credential). This will prompt to enter credentials
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
#Create Site Page
$page = Add-PnPClientSidePage  -Name “PageName”
#Add a new sections to the page 
Add-PnPClientSidePageSection -Page $page -SectionTemplate OneColumn -Order 1 # OneColumnFullWidth is only available if the site is a Communication site
#Add Hero webpart to page  
Add-PnPClientSideWebPart -Page $page -DefaultWebPartType "Hero" -Section 1 -Column 1

After running the code, we need to check site content of the provided site and we will see that a page has been created successfully as shown below.

Finally after opening the site page, go to the edit mode to check the hero Webpart.

Keywords:
• Create Modern site page and add a Webpart to it using PnP Powrshell.
• Creating Modern Page and add hero Webpart using PnP online.
• How to create Modern site page using Pnp Powershell.

Author: Softree Technology

Leave a Reply