Create SharePoint Spaces templates using PnP PowerShell

After a few days exploring SharePoint Spaces I felt the necessity to get a pre-built template instead of starting from scratch every time.

Since SharePoint Spaces are created in the Site Pages library and each component is a web part I’ve tried to create my own template using PnP PowerShell and it worked perfectly.

I’ve built a space to display my magazines, and I want to replicate it to display books and other files I have in my library.

To achieve it I’ve build 2 custom PowerShell scripts that I’m sharing in this article, one to save the template and another one to apply it.

Save a SharePoint Space as a template

The following script creates a template from an existent SharePoint Space, to start the process you will have to:

  • Provide the site collection URL where the Space is located
  • Provide the title the Space
  • Provide the name for the template file

The script will set the page you provided in the setup as the home page of your site collection and will save the site as a template, getting just the page and its content. Once the template is done the original home page of the site collection will be applied again.

pushd (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
$saveDir = (Resolve-path ".\")
Write-Host "Please enter your details"
$siteURL = Read-Host "Site Collection URL:"
$pageTitle = Read-Host "Page Title:"
$templateName = Read-Host "Template Name:"

Write-Host "Connecting to: $siteURL"
Connect-PnPOnline -Url $siteURL
Write-Host "Connected!"
 
$web = Get-PnPWeb
$sourceSite = $web.ServerRelativeUrl
$library = "Site Pages"
 
#get all pages in the site pages library
$pages = Get-PnPListItem -List $library
 
#save current homepage
$currentHomePage = Get-PnPHomePage
 
$pagesList = New-Object System.Collections.Generic.List[System.Object]
 
$pageNumber = 1
 
foreach($page in $pages){
	if($page.FileSystemObjectType -eq "File"){
		$pagePath = $page.FieldValues["FileRef"]
		$pageTitleLibrary = $page.FieldValues["Title"].ToString()
	
		#set space page as home page and save the template		
		if($pageTitleLibrary.ToLower() -eq $pageTitle.ToLower()){
			Set-PnPHomePage -RootFolderRelativeUrl ($pagePath -replace ($sourceSite+"/"), "")	
			Get-PnPProvisioningTemplate -out $($saveDir.Path + "\" + $templateName + ".xml") -Handlers PageContents					 
		}		
	}
}
 

 
#apply default default homepage
Set-PnPHomePage -RootFolderRelativeUrl $currentHomePage
 
#wait a few seconds for the template file 
Start-Sleep -Seconds 15
 
#open main xml
$mainFile = [xml][io.File]::ReadAllText($($saveDir.Path + "\" + $templateName + ".xml"))
 
#remove websettings
$mainFile.Provisioning.Templates.ProvisioningTemplate.RemoveChild($mainFile.Provisioning.Templates.ProvisioningTemplate.WebSettings)
		
$mainfile.Provisioning.Templates.ProvisioningTemplate.ClientSidePages.ClientSidePage.Title = $("##PAGENAME##").ToString()
$mainfile.Provisioning.Templates.ProvisioningTemplate.ClientSidePages.ClientSidePage.PageName = $("##PAGENAME##.aspx").ToString()

#save final tempate
$mainFile.Save($($saveDir.Path + "\" + $templateName + ".xml"))

popd

Create a SharePoint Space based on a template

The following script will create a SharePoint Space in a site collection, before executing it please make sure that the Spaces feature is enabled. To create a Space based on the template you will have to:

  • Provide the site collection URL where you want to create the Space
  • Provide the name for the new page
  • Provide the name of the XML template file
pushd (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
$dir = (Resolve-path ".\")
Write-Host "Please enter your details"
$siteURL = Read-Host "Site Collection URL:"
$pageTitle = Read-Host "Page Title:"
$templateName = Read-Host "Template Name:"

Write-Host "Connecting to: $siteURL"
Connect-PnPOnline -Url $siteURL
Write-Host "Connected!"
 
((Get-Content -path $($dir.Path + "\" + $templateName + ".xml") -Raw) -replace '##PAGENAME##', $pageTitle) | Set-Content -Path 'temp.xml'
 
Apply-PnPProvisioningTemplate -path temp.xml
Remove-Item -Path 'temp.xml'
 
popd

Conclusion

Despite the fact that Spaces are cutting edge technology the existing SharePoint automation tools already support it. All you have to do is use your imagination and take the best out of them.

In the following link you have the two PowerShell scripts shown in the article and the template I did to for my library, if you will use it on your environment let me know the final result in the comments section or on Twitter.

Download Template

Abstract vector created by macrovector – www.freepik.com


No comments yet

Leave a Reply


I've been working with Microsoft Technologies over the last ten years, mainly focused on creating collaboration and productivity solutions that drive the adoption of Microsoft Modern Workplace.

%d bloggers like this: