SharePoint Online + web parts, headers and taxonomy fields.

Translated from the Spanish original. Read in Spanish

In this tutorial, we’ll learn how to automate creating a page in SharePoint Online. The process includes adding taxonomy fields, web parts and a custom header. To do this, we’ll use a PowerShell script and the PnP PowerShell module.

Set up variables

First, we’ll define a few variables needed to connect to SharePoint and to set the path of our header image.

  • TenantId: directory (tenant) ID.
  • AppId: application (client) ID.
  • ClientSecret: client secret.
$TenantId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$AppId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$ClientSecret = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

Connecting to SharePoint

We use Connect-PnPOnline to connect to our SharePoint Online site. In this example, we’re using a managed identity for authentication.

Connect-PnPOnline -Url $siteurl -ManagedIdentity

Define more variables

We specify the location of the header image and the source URL we’ll copy the image from.

$pageRelativeImage="SiteAssets/ZerogapBanner.jpg"
$source_url_header="/sites/ZerogapSite/SiteAssets/Zerogap/ZerogapBanner.jpg"

Check whether the page already exists

Before creating the page, we check whether it already exists using Get-PnPPage.

$full_url_path=$siteurl + "/sitepages/" + $page_name + ".aspx"

$FileExists=$null
$FileExists = Get-PnPPage -identity $page_name -ErrorAction SilentlyContinue

Creating the page and adding components

If the page doesn’t exist, we go on to:

  1. Add a taxonomy field to the “SitePages” list.
  2. Create the new page.
  3. Add a web part to the page.
  4. Copy the header image to the destination folder and set it as the page header.
  5. Disable comments on the page.
  6. Set the content owner.
If($FileExists)
{
    Write-output "Nothing to do for this site:"
    write-output  $siteurl
}

If the page doesn’t exist, we carry on with the rest of the process:

Else {
    Add-PnPTaxonomyField -List "SitePages" -DisplayName "Archive" -InternalName "Tracking" -TaxonomyItemId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    Add-PnPPage  -Name  $page_name
    $page_file=$page_name + ".aspx"

    Add-PnPPageWebPart -Page $page_file -Component "Zerogap Tracking"

    $PageHeaderImage = $relative_site + "/" + $pageRelativeImage
    $target_url=$relative_site + "/SiteAssets"
    $target_url=$target_url.replace('//','/')
    $DocumentLibrary = $null
    $DocumentLibrary = Get-PnPList -Identity "SiteAssets"

    if ($DocumentLibrary -eq $null) {
        $Web = Get-PnPWeb
        $Zerogap = $Web.Lists.EnsureSiteAssetsLibrary()
        $Zerogap = Invoke-PnPQuery
    }

    Copy-PnPFile -SourceUrl $source_url_header -TargetUrl $target_url -Force

    Set-PnPPage -Identity $page_file -CommentsEnabled:$false

    $page=Get-PnPClientSidePage -Identity $page_file
    $page.SetCustomPageHeader($PageHeaderImage)
    $page.Save()
    $Page.Publish()

    Set-PnPPage -Identity $page_file -Publish

    $values=@{"Editor"="admin@Zerogap.com"; "Author"="admin@Zerogap.com"}

    $id = Get-PnPPage -Identity $page_name | Select -ExpandProperty PageId
    $Zerogap = Set-PnPListItem -List "Site Pages" -Identity $id -Values
    $values -UpdateType UpdateOverwriteVersion
    Set-PnPPage -Identity $page_file -Publish
}

Disconnect the session

Finally, once we’ve finished our operations, we disconnect from the SharePoint site.

Disconnect-PnPOnline

Complete code

Below is the complete script, which you can use to automate creating a page in SharePoint Online with all the elements mentioned above.

Param
(
  [Parameter (Mandatory = $true)]  
  [string] $siteurl,
  [Parameter (Mandatory = $true)]
  [string] $page_name,
  [Parameter (Mandatory = $true)]
  [string] $relative_site
)

$TenantId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$AppId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$ClientSecret = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

Connect-PnPOnline -Url $siteurl -ManagedIdentity

$pageRelativeImage="SiteAssets/ZerogapBanner.jpg"
$source_url_header="/sites/ZerogapSite/SiteAssets/Zerogap/ZerogapBanner.jpg"

$full_url_path=$siteurl + "/sitepages/" + $page_name + ".aspx"

$FileExists=$null
$FileExists = Get-PnPPage -identity $page_name -ErrorAction SilentlyContinue

If($FileExists)
{
    Write-output "Nothing to do for this site:"
    write-output  $siteurl
}
Else {
    Add-PnPTaxonomyField -List "SitePages" -DisplayName "Archive" -InternalName "Tracking" -TaxonomyItemId 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
    Add-PnPPage  -Name  $page_name
    $page_file=$page_name + ".aspx"

    Add-PnPPageWebPart -Page $page_file -Component "Zerogap Tracking"

    $PageHeaderImage = $relative_site + "/" + $pageRelativeImage
    $target_url=$relative_site + "/SiteAssets"
    $target_url=$target_url.replace('//','/')
    $DocumentLibrary = $null
    $DocumentLibrary = Get-PnPList -Identity "SiteAssets"

    if ($DocumentLibrary -eq $null) {
        $Web = Get-PnPWeb
        $Zerogap = $Web.Lists.EnsureSiteAssetsLibrary()
        $Zerogap = Invoke-PnPQuery
    }

    Copy-PnPFile -SourceUrl $source_url_header -TargetUrl $target_url -Force

    Set-PnPPage -Identity $page_file -CommentsEnabled:$false

    $page=Get-PnPClientSidePage -Identity $page_file
    $page.SetCustomPageHeader($PageHeaderImage)
    $page.Save()
    $Page.Publish()

    Set-PnPPage -Identity $page_file -Publish

    $values=@{"Editor"="admin@Zerogap.com"; "Author"="admin@Zerogap.com"}

    $id = Get-PnPPage -Identity $page_name | Select -ExpandProperty PageId
    $Zerogap = Set-PnPListItem -List "Site Pages" -Identity $id -Values $values -UpdateType UpdateOverwriteVersion

    Set-PnPPage -Identity $page_file -Publish
}

Disconnect-PnPOnline

This script must be run by a user with the right permissions in SharePoint and with the PnP PowerShell module already installed. Remember to replace the values of the TenantId, AppId and ClientSecret variables, and the taxonomy field ID, with the details of your own SharePoint Online environment.

Maximiliano Díaz Doglia

AI Platform Engineer & Full-Stack Developer
Building Enterprise Integrations & Automations