SharePoint Online: Create Site Design and Site Scripts using PowerShell

What is Site Design and Site Scripts in SharePoint Online?
Site design and site scripts are the new way to create site templates in SharePoint Online. Say, you want to create sites with a specific theme, apply a logo, set regional settings, create a list or library in the site, add users, create a content type and columns, add-remove links from the navigation, trigger a flow, Join to hub-site, add-remove site columns, etc - then make use of site designs. The best part is: Not only for new sites, but you can apply site designs to any existing sites too to deploy the artifacts and make them consistent. Remember, Feature stapling? here too you create site design and register it with SharePoint Online site templates such as team site, or communication site.

A site script is a JSON file with a set of pre-defined action definitions to be executed when you apply site design. A site design acts as a container for the site script(s). You can attach one or more site scripts to site design (and a site script can be attached to more than one site design!), and when you apply the site design, you execute the actions defined in all of the site scripts and they will run in the order listed. Please note, site design is not really a site templates that get attached to the sites - but set of actions executed when you apply a site design. So, changing site designs doesn't change anything on existing sites (until you re-apply the site design).

How to Create a Site Design in SharePoint Online?
Basically creation of site design work like this: You create a site script using JSON template with desired actions to run such as creating lists, applying a theme, etc. and then add the script to your tenant. The next step is to create a site design and add the site script to the design. While creating site design, you have to associate it with any modern site templates such as a team site or communication site template. That's all. Once it's ready, you can apply it either to a new site or existing sites.
  1. Create a site script JSON Schema of required actions as per Microsoft Site design schema reference. You can also use this online tool to generate JSON schema Online JSON Designer: https://www.sitedesigner.io/,  here is my JSON to set a theme, apply a logo, configure regional settings and create a document library.
  2. Use this PowerShell script to create a site script and Site design:
    #Define Sitescript JSON 
    $JSONScript = @"
    {
      "$schema": "schema.json",
      "actions": [
        {
          "verb": "applyTheme",
          "themeName": "Corporate Theme"
        },
        {
          "verb": "setRegionalSettings",
          "timeZone": 24,
          "sortOrder": 25,
          "hourFormat": "12"
        },
        {
          "verb": "setSiteLogo",
          "url": "/Style Library/logo.jpg"
        },
        {
          "verb": "createSPList",
          "listName": "Work",
          "templateType": 101,
          "subactions": [
            {
              "verb": "setTitle",
              "title": "Work"
            }
          ]
        }
      ],
      "bindata": {},
      "version": 1
    }
    "@
    
    #Set Parameters
    $AdminCenterURL = "https://crescent-admin.sharepoint.com/"
    
    #Connect to SharePoint Online
    Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)
    
    #Add Site Script
    $SiteScript = $JSONScript | Add-SPOSiteScript -Title "Crescent Site Script"
    
    #Add Site Design
    $WebTemplate = "64" #64 = Team Site, 68 = Communication Site, 1 = Team Site without Group
    Add-SPOSiteDesign -Title "Crescent Site Template" -WebTemplate $WebTemplate -SiteScripts $SiteScript.ID -Description "Crescent Site Template"
    You can create up to 100 site scripts and 100 site designs per tenant!
  3. Now, you can use this site design either to new sites or you can apply it on existing sites. Here is how it works on new sites:
    1. Go to SharePoint Online Admin center >> Create a new Team site by clicking "Create site" and selecting "Team site" (Since we've created the site design for Team Sites template)
    2. You'll see the custom site design listed in the drop-down. In my case, it is "Crescent Site Template". 
      sharepoint online site design and site scripts
    3. Click on the Finish button to trigger site creation. Once the site is created, you'll see the actions defined in the site script is being applied.
      add site design sharepoint online
  4. To apply the site design on existing sites,  
    1. Go to any site collection of specific site templates (In our case, its "Team Site") >> Click on Settings gear >> Choose the "Site designs" option from the site settings menu.
      sharepoint online create site design
    2. Pick the custom site design we've created from the list and click on the "Apply" button to apply the site design on the current site.
      sharepoint online apply site design
When the actions in the scripts are completed, SharePoint displays detailed results of those actions in a progress pane. Some more PowerShell cmdlets to manage site scripts and site designs in SharePoint Online:
  • Get-SPOSiteDesign - Retrieves all site designs created in the tenant
  • Get-SPOSiteScript - Gets the list of site scripts available
  • Set-SPOSiteDesign - Update existing site design
  • Set-SPOSiteScript - Update the site script JSON
  • Remove-SPOSiteScript - Deletes the given site script
  • Remove-SPOSiteDesign  -Removes the given site design ID
  • Invoke-SPOSiteDesign - Apply site design on existing site
  • Grant-SPOSiteDesignRights - Set permissions to use site designs
  • Revoke-SPOSiteDesignRights - Remove site design permissions
and here are some examples:

Get existing site script and Site Designs:
Get-SPOSiteScript
Get-SPOSiteDesign

Update existing site script:
$SiteScriptId = "e694f7f2-ed9b-48ca-abe4-fb4fedc33c31"
$SiteScriptFile = "c:\Temp\sitescript.json"
Set-SPOSiteScript -Identity $SiteScriptId -Content (Get-Content $SiteScriptFile -Raw)

Update existing site Design:
$SiteDesignId = "4d086b5b-c7a5-4b60-a3c5-e572ff0a6b8d"
Set-SPOSiteDesign -Identity $SiteDesignId -SiteScripts "d4708c15-949c-491f-8503-cd2a67a1de66", "36f5796f-dee2-4983-80af-a08d2951df86"

Apply site Design on Existing Site:
#Set Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com/"
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$SiteDesignId = "4d086b5b-c7a5-4b60-a3c5-e572ff0a6b8d"

#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Apply Site Design
Invoke-SPOSiteDesign -Identity $SiteDesignId -WebUrl $SiteURL

Set Site Design as Default:
Set-SPOSiteDesign -Identity $SiteDesignId -IsDefault:$True

Delete existing site script and site design:
Remove-SPOSiteDesign -Identity "f6611c71-dba5-4a32-8811-51472b7e5961"
Remove-SPOSiteScript -Identity "b2ac88ee-2711-4cbc-bea9-bf63e22af735"
SharePoint Online: Create Site Design and Site Scripts using PowerShell SharePoint Online: Create Site Design and Site Scripts using PowerShell Reviewed by Salaudeen Rajack on October 27, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.