SharePoint Online: Create Publishing Page using PowerShell

Requirement: Create a Publishing Page in SharePoint Online using PowerShell

SharePoint Online: Create Publishing Page using PowerShell CSOM
#Config Variables
$SiteURL = "https://crescenttech.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("", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)
$ListItem.File.Publish("")
$Ctx.ExecuteQuery()
Write-host -f Green "Done!"

PnP PowerShell to Create a Publishing Page in SharePoint Online:
#Config Variables
$SiteURL = "https://crescenttech.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"
 
#Get Credentials to connect
$Cred = Get-Credential
 
Try {
    #Connect to PNP Online
    Connect-PnPOnline -Url $SiteURL # -Credentials $Cred
 
    #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
}
SharePoint Online: Create Publishing Page using PowerShell SharePoint Online: Create Publishing Page using PowerShell Reviewed by Salaudeen Rajack on April 14, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.