Understanding Site Scripts and Site Templates: A Guide to Customizing SharePoint Sites

Introduction

SharePoint Online provides powerful tools for automating site configuration, ensuring consistency across sites, and streamlining site provisioning. One such tool is Site Scripts, which are JSON-based definitions that allow administrators to define various actions when a site is created or modified.


For a complete list of available actions and their parameters, see the JSON schema.

What is a Site Script?

A Site Script is a JSON-based configuration file used in SharePoint Online to automate site setup and customization. Site Scripts work alongside Site Templates (formerly known as Site Designs) to provide a seamless, repeatable process for creating and configuring sites.

What Can a Site Script Do?

  • 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 Scripts work in combination with Site Templates (formerly known as Site Designs) to create consistent and automated site provisioning.

Creating a Site Script using PnP PowerShell

PnP PowerShell is a powerful tool that helps automate SharePoint operations. Below is a step-by-step guide to creating a Site Script using PnP PowerShell.

Step 1: Connect to the SharePoint Admin Center

To start, connect to the SharePoint Admin Center using the following script:


$Tenantname="xyz"
$TenantUrl="https://$Tenantname-admin.sharepoint.com"
$clientId="[client Id]"
#Parameter
$AdminCenterURL = $TenantUrl
 
#Connect to Admin Center
Connect-PnPOnline -Url $AdminCenterURL -Interactive -ClientId $clientId

Step 2: Define the Site Script JSON

Below is an example Site Script JSON that creates a list named Document Creation and adds various fields:

# Define Site Script JSON
$DocumentCreationsiteScriptJson = @'
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json",
    "actions": [
     
        {
            "verb": "createSPList",
            "listName": "Document Creation",
            "templateType": 100,
            "subactions": [
                {
                    "verb": "setDescription",
                    "description": "List to Store the Metadatas of Documents"
                },
                {
                    "verb": "addSPField",
                    "fieldType": "Text",
                    "displayName": "Document Name",
                    "internalName": "DocumentName",
                    "isRequired": true
                },
                {
                    "verb": "addSPField",
                    "fieldType": "DateTime",
                    "displayName": "Approved Date",
                    "internalName": "ApprovedDate",
                    "isRequired": false
                },
               
                {
                    "verb": "addSPField",
                    "fieldType": "User",
                    "displayName": "Approver",
                    "addToDefaultView": true,
                    "isRequired": true
                },
                {
                "verb": "addSPFieldXml",
                    "schemaXml": "<Field Type='Choice' DisplayName='Status'
Name='Status' Required='TRUE'><CHOICES><CHOICE>In Progress</CHOICE>
<CHOICE>Approved</CHOICE><CHOICE>Rejected</CHOICE></CHOICES></Field>"
                },
               {
               "verb": "addSPFieldXml",
               "schemaXml": "<Field DisplayName=\"ProfileImage\"
Format=\"Thumbnail\" IsModern=\"TRUE\" Name=\"ProfileImage\" Title=\"ProfileImage\"
Type=\"Thumbnail\" ID=\"{61552478-86e5-4c12-b9d3-51c29b1d2424}\"
SourceID=\"{a9edeafe-5316-4317-8828-e8db8e02b2d8}\" StaticName=\"ProfileImage\"
ColName=\"ntext2\" RowOrdinal=\"0\" />"
                },

                {
                "verb": "addSPView",
                "name": "Weekly Update View",
                "viewFields":
                [
                   "ID",
                   "Approved Date",
                   "Document Name",
                   "Approved Date"
                ],
                "query": "<OrderBy><FieldRef Name=\"ID\" /><FieldRef Name=\"ID\"
Ascending=\"FALSE\" /></OrderBy>",
                "rowLimit": 100,
                "isPaged": true,
                "makeDefault": true
              }
            ]
        },
       
    ],
    "bindata": {},
    "version": 1
}
'@


Step 3: Add the Site Script to SharePoint

Run the following PowerShell command to add the Site Script:


# Create Site Script
$DCsiteScript = Add-PnPSiteScript -Title "Document Creation Script"
-Content $DocumentCreationsiteScriptJson
Write-Host "DCsiteScript Created: $($siteScript.Id)"


Step 4: Create a Site Template using the Site Script

Once the Site Script is created, use it to generate a Site Template:

# Create Site Template using the Site Script
$siteTemplate = Add-PnPSiteDesign -Title "Document Creation Template"
-WebTemplate "68" -SiteScriptIds $DCsiteScript.Id
-Description "Template for Document Management System"
Write-Host "Site Template Created: $($siteTemplate.Id)"

Step 5: Apply the Site Template to a New Site

Once you run the script successfully, you will see a confirmation message: "Site Template Created".

To apply this template, follow these steps:

  1. Create a new site in SharePoint Online.

  2. Choose Communication Site as the template.

  3. Select From your Organization to apply the custom site template.

  4. Your new site will be provisioned with the predefined settings, including lists, fields, and views.

Conclusion

Site Scripts and Site Templates provide a structured way to automate SharePoint Online site configuration. By using PnP PowerShell, administrators can efficiently create and manage Site Scripts, ensuring consistency across SharePoint sites. Whether setting up metadata structures, applying branding, or integrating automation, Site Scripts help streamline SharePoint site provisioning and enhance the user experience.


Comments