Automate post creation customizations on your sites

site-design-information-panel-applied-site-designs

Note : This customization is only available for SharePoint Online at the moment

When someone create a site in SharePoint Online, you can give them the opportunity to choose custom template inside the Communication and Team site selection :

The templates you see in the picture above are available by default for Communication site creation.
If you are not familiar with Communication and Teams site for SharePoint Online, I recomend you to read this article where we discuss the topic.

To give you some vocabulary, those templates are called Site Design. You can create up to 100 Site Design per Office 365 tenant.
Site Design is in fact a container, the post creation action are contained in Site Scripts. You can create up to 100 Site Scripts.

So when you want to create a new template for your sites, you will first create one or multiple Site Script(s) and a Site Design applying those Site Scripts. This is pretty conveniant as you might want to apply common actions (Site Scripts) in your different Templates (Site Design).

So far, here are the available actions for Site Script and Site design :

-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 Microsoft 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

I would like to focus a little bit on “Triggering a flow”.
In fact this action allow you to do whatever you want, because you can launch a flow when a site is created with you Site Design. This flow can then execute an Azure function giving you quite a lot to work with 🙂
I found this reference pretty usefull if you go in that direction.

How to write you post site creation actions ?

A Site Script is written in Json, following this patern :

{
    "$schema": "schema.json",
    "actions": [
        ...
        
        ...
    ],
    "bindata": { },
    "version": 1
};

You can find the Json reference schema here

An example being easyer to understand, let’s write a Site Script where we want to Create a list with some columns and Set External Sharing.

It will look like this :

 {
     "$schema": "schema.json",
         "actions": [
             {
                 "verb": "createSPList",
                 "listName": "My list",
                 "templateType": 100,
                 "subactions": [
                     {
                         "verb": "SetDescription",
                         "description": "My super list"
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "Text",
                         "displayName": "Customer Name",
                         "isRequired": false,
                         "addToDefaultView": true
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "Number",
                         "displayName": "Requisition Total",
                         "addToDefaultView": true,
                         "isRequired": true
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "User",
                         "displayName": "Contact",
                         "addToDefaultView": true,
                         "isRequired": true
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "Note",
                         "displayName": "Meeting Notes",
                         "isRequired": false
                     }
                 ]
             },
             {
                "verb": "joinHubSite",
                "hubSiteId": "339f4869-d7a5-4e3b-99ac-93f912a75798"
            },
            {
                "verb": "setSiteExternalSharingCapability", 
                "capability": "ExistingExternalUserSharingOnly"
            }
         ],
             "bindata": { },
     "version": 1
 }

In this Json code, we can see that we create a first action to Create the list (CreateSPList, with the name “My List” and the baseTemplate “100” for a custom sharepoint list). Then we set sub-actions for this list to set the description (SetDescription) and create 3 columns (addSPField) by giving them a name, type and some extra properties.

After that we ask the site to join a HubSite (joinHubSite). You can read this article for more information on this.

And finally we set the external sharing policy to “ExistingExternalUserSharingOnly” (setSiteExternalSharingCapability).

How to deploy in your environment ?

We are going to deploy this Site Script using PowerShell and the Microsoft.Online.SharePoint.PowerShell Module :

#Connecting to SharePoint
Connect-SPOService -Url "https://btcstech-admin.sharepoint.com"
#Creating the Site Script Content
$siteScriptContent = '
 {
     "$schema": "schema.json",
         "actions": [
             {
                 "verb": "createSPList",
                 "listName": "My list",
                 "templateType": 100,
                 "subactions": [
                     {
                         "verb": "SetDescription",
                         "description": "My super list"
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "Text",
                         "displayName": "Customer Name",
                         "isRequired": false,
                         "addToDefaultView": true
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "Number",
                         "displayName": "Requisition Total",
                         "addToDefaultView": true,
                         "isRequired": true
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "User",
                         "displayName": "Contact",
                         "addToDefaultView": true,
                         "isRequired": true
                     },
                     {
                         "verb": "addSPField",
                         "fieldType": "Note",
                         "displayName": "Meeting Notes",
                         "isRequired": false
                     }
                 ]
             },
             {
                "verb": "joinHubSite",
                "hubSiteId": "339f4869-d7a5-4e3b-99ac-93f912a75798"
            },
            {
                "verb": "setSiteExternalSharingCapability", 
                "capability": "ExistingExternalUserSharingOnly"
            }
         ],
             "bindata": { },
     "version": 1
 }
 '
#Creating the Site Script
$siteScript = Add-SPOSiteScript -Title "Demo" -Content $siteScriptContent

Now that our Site Script is available, we will create a Site Design to enable users to chose it from the Create Site menu :

#Retrieving the preview picture to be displayed
$previewUrl = "https://btcstech.sharepoint.com/sites/NewHubsite/Shared%20Documents/hubSite preview.png"
#Creating the Site Dcript (WebBaseTemplate 64 for Team Site and 68 for Communication Site
Add-SPOSiteDesign -Title "Demo Site Design" -WebTemplate "64" -SiteScripts $siteScript.Id -Description "Demo showing list creation and external sharing" -PreviewImageUrl $previewUrl -PreviewImageAltText "site preview"

Now, everyone allowed to create a Site in the company will be able to choose this Site Design from the site creation menu :

Site creation with site design

Bear in mind that those actions are post-creation actions. It means that the site will be created and available before those actions are triggered. The user will see a message that actions are currently running if he connects directelly to the site. He will also be able to follow the progress and be informed if there is an issue on actions.

Site Design being applyed

I would like to end this article by talking about three interesting points :

First is the fact that you can scope each of those Site Design. It means that you can control which population will be able to use them. You can perform scoping with the following CmdLet :

Grant-SPOSiteDesignRights -Identity db752673-18fd-44db-865a-aa3e0b28698e -Principals ("accounting@contoso.sharepoint.com") -Rights View

Second point is that you can browse Site Script examples in the community based ressource on git hub

And the last point, still experimental, is that you can extract a Site Script from an existing site, this one is awesome 🙂
You can watch this short video to see how it works :

I hope you took as much interest reading this article as I did writing it.

See you soon in the blog section ! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.