Save and Deploy SharePoint Page Templates Using Script
When managing a SharePoint environment, particularly with client-side pages, automating repetitive tasks such as exporting templates can be a real time-saver. Recently, I had to export SharePoint page templates and save them with meaningful filenames instead of generic names like page1.xml
. After some experimentation, I developed a PowerShell script that saves these templates using their page name or title.
Here’s how you can do it too.
Prerequisites
Before you begin, ensure you have:
- Installed the PnP PowerShell module.
- Sufficient permissions to connect to your SharePoint site and export templates.
- The necessary Client ID for authentication.
Save SharePoint Template
This PowerShell script automatically exports SharePoint page templates and saves them using the page’s name or title, instead of generic filenames like page1.xml
.
$siteCollection = "https://your-sharepoint-site-url"
$saveTemplateLocation = "E:\" # Folder to save the templates
# Connect to the SharePoint site
Connect-PnPOnline -Url $siteCollection -Interactive -ClientId <Your-Client-ID>
# Get the site template with client-side pages
$siteTemplate = Get-PnPSiteTemplate -IncludeAllClientSidePages -Handlers Pages,PageContents -OutputInstance
# Get the site template with all client-side pages
$siteTemplate = Get-PnPSiteTemplate -IncludeAllClientSidePages -Handlers Pages,PageContents -OutputInstance
# Loop through all client-side pages and filter out the pages promoted as templates
foreach($page in $siteTemplate.ClientSidePages)
{
if($page.PromoteAsTemplate -eq $true) # Check if the page is promoted as a template
{
# Use the PageName or Title property for the file name
$pageName = if ([string]::IsNullOrEmpty($page.PageName)) { $page.Title } else { $page.PageName }
# If both are null or empty, fall back to a generic name
if ([string]::IsNullOrEmpty($pageName)) {
$pageName = "PageTemplate" + $pageCounter
}
# Initialize a new template object for each page
$individualTemplate = New-PnPSiteTemplate
# Add the current page to this new template
$individualTemplate.ClientSidePages.Add($page)
# Replace any illegal characters for file names
$fileName = ($pageName -replace '[\/:*?"<>|]', '') + ".xml"
# Generate the full path for the file
$filePath = Join-Path -Path $saveTemplateLocation -ChildPath $fileName
# Save each template individually using the page's name or title
Save-PnPSiteTemplate -Template $individualTemplate -Out $filePath
Write-Host "Saved template as: $filePath"
}
}
Deploy SharePoint Templates
Once your templates are saved, you can deploy them back to your SharePoint site with this simple script:
$siteCollection = "https://your-sharepoint-site-url"
$templateFileLocation = "E:\YourTemplate.xml"
# Connect and deploy the template
Connect-PnPOnline -Url $siteCollection -Interactive -ClientId <Your-Client-ID>
Invoke-PnPSiteTemplate -Path $templateFileLocation
Key Points:
- Dynamic Filenames: Templates are saved using the page's
PageName
orTitle
. - Deployment: Use
Invoke-PnPSiteTemplate
to deploy the saved templates back to SharePoint. - File Name Cleanup: Invalid characters are removed for filesystem compatibility.
Conclusion
With these scripts, you can efficiently save and deploy SharePoint page templates, ensuring they're organized and easy to work with. This automation is perfect for managing multiple client-side pages in SharePoint at scale.