SharePoint Sites Templates – from the battlefield
# Install Required Modules
Install-Module PnP.PowerShell -RequiredVersion 1.12.0 -Force
Install-Module PnP.PowerShell# Register AppID
Register-PnPEntraIDAppForInteractiveLogin -ApplicationName “PnP PowerShell” -SharePointDelegatePermissions “AllSites.FullControl” -Tenant xxx.onmicrosoft.com
# Connect to the SharePoint Online
$adminSiteUrl = “https://xxx-admin.sharepoint.com/”
$siteUrl = “https://xxx.sharepoint.com/sites/Test01”Connect-PnPOnline $adminSiteUrl -Interactive -ClientId xxx
# Test connection (display all SharePoint Online Sites)
Get-PnPTenantSite
# Get the json template from example site that we want to create template.
Get-PnPSiteScriptFromWeb -Url $siteUrl -IncludeAll > template.json
# Create template
$siteScriptFile = $PSScriptRoot + “.\template.json”
$webTemplate = “64” #64 = Team Site, 68 = Communication Site, 1 = Groupless Team Site
$siteScriptTitle = “Team01 Team Site Script”
$siteDesignTitle = “Team01 Team Site Template”
$siteDesignDescription = “Custom team site template with multi-colored theme, external sharing disabled and some cool stuff via Power Automate.”$designPackageId = “6142d2a0-63a5-4ba0-aede-d9fefca2c767” # The default site template to use as a base when creating a communication site, more info later.
$siteScript = (Get-Content $siteScriptFile -Raw | Add-PnPSiteScript -Title $siteScriptTitle) | Select -First 1 Id
Add-PnPSiteDesign -Title $siteDesignTitle -SiteScript $siteScript.Id -WebTemplate $webTemplate -Description $siteDesignDescription -DesignPackageId $designPackageId
# Now you can apply template using GUI
# Display information about scripts and designs
Get-PnPSiteDesign
Get-PnPSiteScript
# Delete the template
Remove-PnPSiteDesign -Identity xxx
Remove-PnPSiteScript -Identity xxx
# Export Home.aspx design
Connect-PnPOnline $SiteUrl -Interactive -ClientId xxx
Export-PnPPage -Identity Home.aspx -Configuration -Out home.xml
# Apply Home.aspx design
$destUrl=”https://xxx.sharepoint.com/sites/Team04″
Connect-PnPOnline $destUrl -Interactive -ClientId xxx
Invoke-PnPSiteTemplate -Path .\home.xml
The above can be invoked as Azure Function.
A lot of Tips & Tricks can be found here.