GitHub Icon Image
GitHub

Add Page template to Site

Summary

This sample script shows how to add a page template to a SharePoint Online site collection using PnP PowerShell and CLI for Microsoft 365.

Example Screenshot

Implementation

  • Open Visual Studio Code
  • Create a new ps1 file
  • Write a script as below
  • Run the script like in the .Example of the Synopsis
  • PnP PowerShell
  • CLI for Microsoft 365
<# .SYNOPSIS
    Provision a page templates.

.DESCRIPTION
    You need to have the latest version of PnP PowerShell

    Provision a single page template to a SharePoint Online site collection.

.PARAMETER SiteCollection
    Specifies the URL of the SharePoint Online Site Collection.

.PARAMETER PageName
    Specifies the name of the new page template.

.EXAMPLE
    PS> .\add_spopagetemplate.ps1 -SiteCollection "https://contoso.sharepoint.com" -PageName "Contoso Page Template"
  
#>

param ([Parameter(Mandatory)]$SiteCollection,[Parameter(Mandatory)]$PageName)

# Variables
$LogFileName = "add_spopagetemplates_" + $(get-date -f filedatetime) + ".txt"

#####################
Start-Transcript -Path .\logs\$LogFileName -NoClobber 

Connect-PnPOnline -Url $SiteCollection -Interactive

Add-PnPPage -Name $PageName -HeaderLayoutType FullWidthImage -PromoteAs Template -Publish

Disconnect-PnPOnline

#####################
Stop-Transcript

Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell

The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?


# SharePoint online site URL
$siteUrl = "https://contoso.sharepoint.com/sites/SPConnect"

# Name of page template to add
$pageTemplateName = "pagetemplate.aspx"

# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
   m365 login
}

# Add page template to SharePoint online site
m365 spo page add --name $pageTemplateName --webUrl $siteUrl --promoteAs Template

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

Contributors

Author(s)
@Expiscornovus
Ganesh Sanap

Disclaimer

THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

Back to top Script Samples
Generated by DocFX with Material UI