SharePoint Online: Create Publishing Page using PowerShell

Requirement: Create a Publishing Page in SharePoint Online using PowerShell.

SharePoint Online: Create a Publishing Page using PowerShell CSOM

Let me show you how to create a publishing page using PowerShell. Make sure you have the publishing feature enabled before running this script:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$PageName = "About-us.aspx"
$PageTitle= "About Us"
$PageLayoutName = "BlankWebPartPage.aspx"
$PageContent="Out Fund brings together healthcare technology companies, foundations, development financing institutions and institutional investors,`
              to address and impact poor healthcare outcomes in Africa and Asia. Its healthcare investments include Care Hospitals in India"

#Get Credentials to connect
$Cred= Get-Credential
 
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Get the publishing Web  
$PublishingWeb = [Microsoft.SharePoint.Client.Publishing.PublishingWeb]::GetPublishingWeb($Ctx, $Ctx.Web)  
$ctx.Load($PublishingWeb) 
$Ctx.ExecuteQuery()

Write-host -f Yellow "Getting Page Layout..." -NoNewline
#Get the Page Layout
$RootWeb = $Ctx.Site.RootWeb
$MasterPageList = $RootWeb.Lists.GetByTitle('Master Page Gallery')
$CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
$CAMLQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>$PageLayoutName</Value></Eq></Where></Query></View>"
$PageLayouts = $MasterPageList.GetItems($CAMLQuery)
$Ctx.Load($PageLayouts)
$Ctx.ExecuteQuery()
$PageLayoutItem = $PageLayouts[0]
$Ctx.Load($PageLayoutItem)
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

#Create Publishing page
Write-host -f Yellow "Creating New Page..." -NoNewline
$PageInfo = New-Object Microsoft.SharePoint.Client.Publishing.PublishingPageInformation  
$PageInfo.Name = $PageName
$PageInfo.PageLayoutListItem = $PageLayoutItem 
$Page = $PublishingWeb.AddPublishingPage($PageInfo)  
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

#Get the List item of the page
Write-host -f Yellow "Updating Page Content..." -NoNewline
$ListItem = $Page.ListItem
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Update Page Contents
$ListItem["Title"] = $PageTitle
$ListItem["PublishingPageContent"] = $PageContent
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

#Publish the page
Write-host -f Yellow "Checking-In and Publishing the Page..." -NoNewline
$ListItem.File.CheckIn([string]::Empty, [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$ListItem.File.Publish([string]::Empty)
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

PnP PowerShell to Create a Publishing Page in SharePoint Online:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$PageName = "About-us"
$PageTitle= "About Us"
$PageTemplate = "BlankWebPartPage"
$PageContent="Out Fund brings together healthcare technology companies, foundations, development financing institutions and institutional investors,`
              to address and impact poor healthcare outcomes in Africa and Asia. Its healthcare investments include Care Hospitals in India"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive
 
    #Create a publishing page
    Add-PnPPublishingPage -PageName $PageName -Title $PageTitle -PageTemplateName $PageTemplate
    
    #Get the Page
    $Page = Get-PnPListItem -List "Pages" -Query "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$PageTitle</Value></Eq></Where></Query></View>"

    #check-out the page for editing
    Set-PnPFileCheckedOut -Url $Page["FileRef"]
 
    #Set Page Content
    Set-PnPListItem -List "Pages" -Identity $Page -Values @{"PublishingPageContent"=$PageContent}
 
    #check-in the page
    Set-PnPFileCheckedIn -Url $Page["FileRef"] -CheckinType MajorCheckIn
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

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 “SharePoint Online: Create Publishing Page using PowerShell

  • Hi,
    This is a great post. Is there a way for us to extract all content from a publishing page, i.e. all text with html tags, image link and videos?
    Thanks

    Reply

Leave a Reply

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