Apr 05 2023 03:21 AM
Apr 05 2023 03:21 AM
As I've been working on this for a recent ptoject, let me share with you this Step by Step guide to create a SPO template and apply it to new/existing SPO sites.
- We need to identify the site URL that we would like to use as template. ( Example: https://company.sharepoint.com/sites/TheDesiredSite ).
- Connect to SPO from PowerShell, ( Connect-SPOService ), and create the necessary SiteScripts that we’ll need later to create our SiteDesign.
Basically, what we do in this step is to download and create the required scripts from our site in order to apply it later as a Custom template for new sites.
$extracted = Get-SPOSiteScriptFromWeb -WebUrl https://contoso.sharepoint.com/sites/template -IncludeBranding -IncludeTheme -IncludeRegionalSettings -IncludeSiteExternalSharingCapability -IncludedLists ("Shared%20Documents", "Lists/Project%20Activities") Add-SPOSiteScript -Title "Contoso template site" -Description "This is a copy of a site collection." -Content $extracted
You’ll get an output like the following one. IMPORTANT: Note the ID as we’ll need it later for our SiteDesign.
- If we need only the configuration/Script of a list, ( or many lists ), we can do it like following: ( OPTIONAL )
$extracted = Get-SPOSiteScriptFromList -ListUrl https://contoso.sharepoint.com/sites/strategy/customer-contacts Add-SPOSiteScript -Title "Contoso Customer Tracker" -Description "This creates a customer contact list" -Content $extracted
IMPORTANT: We can create as many we need for our case, for example one WebScript and different ListScripts from other sites if we need those list integrated.
- Now we have created the different SiteScripts that we need for our template. At this point, we need to add our custom Site Design to the tenant.
Add-SPOSiteDesign -Title "DesiredName" -WebTemplate 64 -SiteScripts 2bf13764-19be-4ac4-9463-cb55d6fb1a55
IMPORTANT: Use WebTemplate 64 for Team Sites and WebTemplate 68 for Communication Sites.
The SiteScripts switch can be a single one or many ones separated by comma. The scripts run in the order listed.
You can use more options to personalize the template, like Description, Preview Image, Thumbnail Image… Example:
Add-SPOSiteDesign -Title "Contoso customer tracking" -WebTemplate "64" -SiteScripts "<ID>" -Description "Tracks key customer data in a list" -ThumbnailUrl "https://contoso.sharepoint.com/SiteAssets/site-thumbnail.png" -PreviewImageUrl "https://contoso.sharepoint.com/SiteAssets/site-preview.png" -PreviewImageAltText "site preview"
- Your created SiteDesign is now available to be implemented by PowerShell and also for the users when they create a new SPO site, from the Organization Templates option:
- In order to create a new PSO site from PowerShell and apply our Custom template, we can simply run the New-SPOSite with our desired options and then the Invoke-SPOSiteDesign cmdlt.
New-SPOSite -Url https://domain.sharepoint.com/sites/Test8 -title Test8 -Owner email address removed for privacy reasons -StorageQuota 2048 -Template STS#3 -NoWait Invoke-SPOSiteDesign -Identity b9f0e526-583d-445a-b92b-664f494e42b6 -WebUrl https://domain.sharepoint.com/sites/Test8
IMPORTANT: For the “Invoke” cmdlt, the “Identity” is the ID that we copied from our created SiteDesign and the “WebUrl” is our destination site, the new created one.
This will execute on your new created SPO site all the Site Scripts added to your Site Design, adding/adapting all the required content. Once done, the PS output will be something like this:
- Available templates to use with the New-SPOSite cmdlt:
Now the new site is ready to use :)