Working with Site Scripts and Site Templates

Introduction

Site scripts and Site Templates are a way to apply custom configurations on top of existing sites to keep them uniform across the tenant with respect to List Templates, Themes, Branding, External Sharing Settings, etc:

Site scripts

Site scripts are JSON files that specify an ordered list of actions to run when creating the new site. The actions are run in the order listed. The following example is a script that has one action verb: createSPList which would create a list by the name Covid Protection and create the defined site columns, content type as part of it.

Text

Description automatically generated

Each action in a site script is specified by a verb value in the JSON. In the previous script, the action is specified by the createSPList verb which creates the list. We can use various other available verbs to achieve the functionalities like :

  • Creating a new list or library (or modifying the default one created with the site)
  • Creating site columns, content types, and configuring other list settings
  • Set site branding properties like navigation layout, header layout and header background
  • Applying a theme
  • Setting a site logo
  • Adding links to quick launch or hub navigation
  • Triggering a Power Automate flow
  • Installing a deployed solution from the app catalog
  • Setting regional settings for the site
  • Adding principals (users and groups) to SharePoint roles
  • Setting external sharing capability for the site

Site Templates

Site templates were previously called Site Designs. It acts as a wrapper and container for the actions defined in the Site Script. Site templates can be used each time a new site is created to apply a consistent set of actions defined in the site script. Application of Site design to an existing site can be done out of the box as well as programmatically. Out of the box, the option is available in Site Settings

Graphical user interface, application, Word

Description automatically generated

Selecting Apply a Site Template will give us OOB Site Templates as well as the Custom Site Templates we have created.

Graphical user interface, website

Description automatically generated

In this article, we will see how to extract the Customized Site actions as a Site Script and create a Site Template out of it so that it can be reused across sites. We can extract Themes, branding, regional settings, External Sharing settings, List and columns as part of the Site Script and create a Site design.

Extracting Site Script

We can extract the Site Script from an existing site using the Get- SPOSiteScriptFromWeb cmdlet which has the syntax:

Get-SPOSiteScriptFromWeb
[-WebUrl] <string>
[-IncludedLists <string[]>]
[-IncludeBranding]
[-IncludeTheme]
[-IncludeRegionalSettings]
[-IncludeSiteExternalSharingCapability]
[-IncludeLinksToExportedItems]
[<CommonParameters>]

We will extract the Theme 1 Library and 1 List from the source site using the below command

$extractedContent = Get-SPOSiteScriptFromWeb `
-WebUrl https://office365journey.sharepoint.com/sites/2021CovidActions/ `
-IncludeTheme `
-IncludedLists (“Covid Prevention”, “Lists/Covid Medicine Inventory”)
Text

Description automatically generated

We will then add the Site Script to SharePoint Online using the Add-SPOSiteScript command by passing in the extracted JSON verb as a parameter along with the title and description

Add-SPOSiteScript `
-Title “Covid Actions” `
-Description “This is a copy of Covid Action Site” `
-Content $extractedContent
Text

Description automatically generated

Copy the ID of the Site Script as we will be using in the next step

Add Site Template/Design

Finally let’s apply the site Design by using the command Add-SPOSiteDesign and pass the Title, Web Template, and Site Script as the parameters.

Text

Description automatically generated

Apply the Site Template

Once we head over to Site Settings ->Apply Site Template, we can see the recently added Site Template which houses the extracted site script actions.

Graphical user interface, application

Description automatically generated

Selecting Covid Action Site Template will show the actions that will be applied which shows the application of theme and provisioning of the list and library.

Table

Description automatically generated

Clicking on the use template will apply it on top of the existing site. We can see that the existing Blue theme has been applied and the list and library are now present on the new Site.

Graphical user interface, table

Description automatically generated

Summary

Thus we saw how to extract a Site Script out of an existing site and create a Site Design/template out of it, add it to the SharePoint Online collection and later apply it on top of an existing site.

Related Articles

Author

Author

Priyaranjan KS is a Modern Workplace Architect primarily focused on developing and architecting solutions around Office 365,Power Platform and Azure.He is also a Microsoft Most Valuable Professional(MVP) and a Microsoft Certified Trainer(MCT)

Latest Articles