Site Scripts And Site Designs

What is Site Design

 
Site Design is a template for the SharePoint site. We can apply pre-defined actions, theme, or SPFX solutions when we create a new site. After creating the site, we can bind site script with site Design to perform certain actions like creating lists, add users to Groups, apply site logo, etc.
 

What is Site Script?

 
Site Script are action commands which can be attached with Site Design to apply when the site will be created. It contains certain actions like creating lists, adding SPFx application to the site, adding users in Groups, adding the site logo, and applying themes, when we create the site script.
 

Apply Site Design

 
We can apply site designs and register them in SharePoint to any of the modern sites. It can be the Team site or the Communication site. We can follow the below steps.
  1. Go to the SharePoint start page on our SharePoint developer tenant.

    Site Scripts And Site Designs

  2. Click Create site from the ribbon. We can see two modern template sites: Team site and Communication site.
  3. Select Communication site,

    Site Scripts And Site Designs
By default, the communication site will have three options given below.
  • Topic
  • Showcase
  • Blank
Site Scripts And Site Designs
 
We can select from existing Site Design and create a new site.
 

Structure of Site Script

 
Site scripts are JSON objects that contain a list of actions to execute when we create a new site. These actions will execute in order.
 
Below script is an example which has two top-level actions. The first task it will perform is to apply theme which is already created in our tenant with Demo Theme. Then it will create a list with name Demo List.
  1. {  
  2.     "$schema""https://developer.microsoft.com/json-schemas/sp/site-design-script-actions.schema.json",  
  3.     "actions": [{  
  4.         "verb""applyTheme",  
  5.         "themeName""Demo Theme"  
  6.     }, {  
  7.         "verb""createSPList",  
  8.         "listName""Demo List",  
  9.         "templateType": 100,  
  10.         "subactions": [{  
  11.             "verb""SetDescription",  
  12.             "description""Demo purpose"  
  13.         }, {  
  14.             "verb""addSPField",  
  15.             "fieldType""Text",  
  16.             "displayName""Name",  
  17.             "isRequired"false,  
  18.             "addToDefaultView"true  
  19.         }, {  
  20.             "verb""addSPField",  
  21.             "fieldType""Number",  
  22.             "displayName""Marks",  
  23.             "addToDefaultView"true,  
  24.             "isRequired"true  
  25.         }, {  
  26.             "verb""addSPField",  
  27.             "fieldType""User",  
  28.             "displayName""Contact",  
  29.             "addToDefaultView"true,  
  30.             "isRequired"true  
  31.         }, {  
  32.             "verb""addSPField",  
  33.             "fieldType""Note",  
  34.             "displayName""Meeting Notes",  
  35.             "isRequired"false  
  36.         }]  
  37.     }],  
  38.     "version": 1  
Every action in Site Script has a verb which describes a type of action. For example, in the above Script, the first action is used by the applyTheme verb. The other is createSPList verb which will create the list. createSPList verb contains its own sub verbs for creating columns.
 

Create Site Script using PowerShell 

 
Note
First Run Connect-SPOService before executing other commands and enter SharePoint admin center URL and authenticate it.
 
Add Site Theme
  1. $themeObject = @ {  
  2.     "themePrimary" = "#003E5E"  
  3.     "themeLighterAlt" = "#cfe2ec";  
  4.     "themeLighter" = "#a8c9da";  
  5.     "themeLight" = "#84b2c8";  
  6.     "themeTertiary" = "#649bb7";  
  7.     "themeSecondary" = "#4986a5";  
  8.     "themeDarkAlt" = "#317293";  
  9.     "themeDark" = "#1d6082";  
  10.     "themeDarker" = "#0c4f70";  
  11.     "neutralLighterAlt" = "#f8f8f8";  
  12.     "neutralLighter" = "#f4f4f4";  
  13.     "neutralLight" = "#eaeaea";  
  14.     "neutralQuaternaryAlt" = "#dadada";  
  15.     "neutralQuaternary" = "#d0d0d0";  
  16.     "neutralTertiaryAlt" = "#c8c8c8";  
  17.     "neutralTertiary" = "#c2c2c2";  
  18.     "neutralSecondary" = "#858585";  
  19.     "neutralPrimaryAlt" = "#4b4b4b";  
  20.     "neutralPrimary" = "#333333";  
  21.     "neutralDark" = "#272727";  
  22.     "black" = "#1d1d1d";  
  23.     "white" = "#ffffff";  
  24.     "primaryBackground" = "#fff";  
  25.     "primaryText" = "#2a2723";  
  26.     "bodyBackground" = "#fff";  
  27.     "bodyText" = "#2a2723";  
  28.     "disabledBackground" = "#f4f4f4";  
  29.     "disabledText" = "#c8c8c8";  
Add-SPOTheme-Name"DemoTheme"-Palette$themeObject-IsInverted$true
 
The above PowerShell Script will add a theme in Tenant with the name DemoTheme.
 
Add Site Script
  1. $siteScript = '{  
  2. "$schema""schema.json""actions": [{  
  3.     "verb""applyTheme",  
  4.     "themeName""DemoTheme"  
  5. }, {  
  6.     "verb""associateExtension",  
  7.     "title""DemoExtension",  
  8.     "location""ClientSideExtension.ApplicationCustomizer",  
  9.     "clientSideComponentId""AFAC4EF5-E65B-455C-A874-BAECDADB6CF9",  
  10.     "scope""Web"  
  11. }, {  
  12.     "verb""setSiteLogo",  
  13.     "url""https://<sharepoint url here>/sites/contentTypeHub/Style%20Library/demo/images/icon.png"  
  14. }, {  
  15.     "verb""addPrincipalToSPGroup",  
  16.     "principal""c:0-.f|rolemanager|spo-grid-all-users/f8c2fa1c-5028-4cd4-b26f-5dd8b8155885",  
  17.     "group""Visitors"  
  18. }, {  
  19.     "verb""addPrincipalToSPGroup",  
  20.     "principal""",  
  21.     "group""Owners"  
  22. }, {  
  23.     "verb""createSPList",  
  24.     "listName""Demo List ",  
  25.     "templateType": 119,  
  26.     "subactions": [{  
  27.         "verb""addContentType",  
  28.         "name"" "  
  29.     }, {  
  30.         "verb""setTitle",  
  31.         "title""DemoList"  
  32.     }]  
  33. }], "version": 1  
  34. }  
  35. '  
Add-SPOSiteScript-Title"DemoScript"-Content$siteScript-Description"Demo Description"
 
Result
 
Id : 7baa37a9-6f0e-44cc-ab32-31cb462a4b73
Title : DemoScript
Description : Demo Description
Content :
Version : 0
 
The above Script will add Site Script with the name DemoScript, which contains different actions like applying DemoTheme to site which we have added earlier, setting site logo, applying Spfx application to the site and creating a certain list to the site. After executing this script, we will get on the ID we need to store that ID to add in site design.
 

Add Site Design

 
We need to place Site Script ID that we have stored in this script then execute the script.
 
Add-SPOSiteDesign-Title"Demo Site Design"-WebTemplate"68"-SiteScripts"7baa37a9-6f0e-44cc-ab32-31cb462a4b73"-Description"Demo Design Description"
 
Result
 
Id : a9eefaec-4305-4d0c-b235-be3a39014a71
Title : Demo Site Design
WebTemplate : 68
SiteScriptIds : {7baa37a9-6f0e-44cc-ab32-31cb462a4b73}
Description : Demo Design Description
PreviewImageUrl :
PreviewImageAltText :
IsDefault : False
Version : 1
 
It will return the ID of the Site Script we have added. Using it we can update the details of Site Design using Set-SPOSiteDesign commands. We can also update site script using Set-SPOSiteScript command.
 
Created site design will be added to the tenant. We can use it while creating a new site or can also apply it on existing sites.
 
Site Scripts And Site Designs
 
The theme that we’ve created will be applied to a newly created site. Also, the site logo will be applied.
 
Site Scripts And Site Designs