Overview
The Modern site offers two out of the box templates - Team Site and Collaboration Site. These templates can be extended by using Site Designs to provision additional SharePoint artifacts on the site.
The Site design can be based on either Team site or Collaboration site. Site design can be associated with one or multiple site scripts. Each site script can have series of actions that gets executed.
SharePoint Online Site Creation Process
- Open SharePoint tile (https://tenant.sharepoint.com/_layouts/15/sharepoint.aspx)
- It offers an option to create a new Modern SharePoint site.
- The available site designs (out of box and custom) appears in site creation process.
- After selection of site design, providing site name and option site description, click Finish.
- During the site design application process, the Modern site with base site design gets created. During the site creation process, the selected site design applies the associated site scripts.
- After the site scripts have finished running, we will see the updates to the site.
Limitation of Site Scripts
- The actions in site script are limited and do not fit all possible scenarios, for example adding footer to every page or creating pages.
- The Site script has a limit of 30 cumulative actions that can be specified across one or more site scripts including sub actions. If we want to execute more than 30 actions to suffice our business requirements, we have to raise our voice to Microsoft with business justification.
However, during the time we are not limited. The site script supports calling Microsoft flows. We can extend this functionality to provision additional artifacts to SharePoint site.
- {
- "$schema": "schema.json",
- "actions": [
- {
- "verb": "triggerFlow",
- "url": "[paste the workflow trigger URL here]",
- "name": "Apply Template",
- "parameters": {
- "event":"",
- "product":""
- }
- }
- ],
- "bindata": {},
- "version": 1
- }
Site Design Extensibility Process
The below diagram explains the process to extend the site design using Microsoft flow.
We will require below components to setup,
- Site design and associated site script
- Microsoft Flow
- Azure Storage Queue
- Azure Function
- PnP Provisioning template
- PowerShell Script
- App ID and App secret with administrative rights on Office 365 tenant
Step 1 - Setup App Only Access to Office 365 Tenant
Setup on normal SharePoint site
- Open SharePoint root site. You may also perform this on any other SharePoint site other than root site
- Navigate to url https://[tenant].sharepoint.com/_layouts/15/appregnew.aspx
- Generate new Client Id and Client Secret by clicking Generate button next to it.
- Enter title for app. Example: "Extend Site Design"
- In the App Domain, specify localhost
- In the redirect URI, specify https://localhost
- Click Create
Note down the Client ID and Client Secret for future reference.
Trust the App for appropriate access to tenant
- Open SharePoint central admin site.
- Navigate to https://[tenant]-admin.sharepoint.com/_layouts/appinv.aspx
- Paste the Client Id to App Id text box
- Click Lookup
- Paste below XML in Permission Request XML textbox
- <AppPermissionRequests AllowAppOnlyPolicy="true" >
- <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />
- </AppPermissionRequests>
- Click Create
- Click Trust It
Step 2 - Create Azure Storage Queue
Azure storage queue will receive a message from Microsoft Flow. When there will be a new message in the queue, an Azure function will be triggered to execute PowerShell script.
- Open Azure Portal https://portal.azure.com
- Click Create a resource
- Select Storage > Storage account - blob, file, table, queue
- Provide the values by filling up the form
- Click Create
- Open Storage account, navigate to Queues.
- Create queue with name sitedesignqueue
- Click Access keys under Settings
- Note down Storage Account Name and the key1 Key value for future reference.
Step 3 - Setup Microsoft Flow
- Open SharePoint site
- In the next tab navigate to MS Flow (https://flow.microsoft.com)
- Click My flows
- Click Create from blank
- From search results, select Request - When an HTTP Request is received
- Search Request
- Select Request - When a HTTP Request is received
- Enter below JSON as request body
- {
- "type": "object",
- "properties": {
- "webUrl": {
- "type": "string"
- },
- "parameters": {
- "type": "object",
- "properties": {
- "event": {
- "type": "string"
- },
- "product": {
- "type": "string"
- }
- }
- }
- }
- }
- Click New Step
- Select Add an action
- Search Azure Queues
- Select Azure Queues - Put a message on a queue
- Enter any name for connection
- Enter storage account name
- Enter storage shared key
- Click Create
- Select the queue name
- In the message, select webUrl
- Click Save
- At the end, the flow will look like below
- Note down HTTP POST URL for future reference.
Test the flow
Run below PowerShell
- $uri = "[Flow URI]"
- $body = "{webUrl:'siteurl'}"
- Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Body $body
See the run history, if flow is successful.
Step 4 - Create PnP provisioning template
Connect to SharePoint Modern site using PnP PowerShell
- Connect-PnPOnline -Url "SharePoint site url"
Run the below command to generate the provisioning template from the site
- Get-PnPProvisioningTemplate -Out .\CustomTemplate.xml -ExcludeHandlers Navigation, ApplicationLifecycleManagement -IncludeNativePublishingFiles
Make edits to the xml or use attached FlowSiteDesignTemplate.xml
Step 5 - Setup Azure Function
- Open Azure Portal https://portal.azure.com
- Click Create a resource
- Select Compute > Function App
- In Storage field, select Use existing and select the storage account created earlier.
- Click Create
- Open Function app. Select Functions > New function
- From language dropdown, select PowerShell
- Select QueueTrigger - PowerShell
- Name function as ApplyPnPProvisioningTemplate
- Enter the queue name created earlier
- Click Create
Step 6 - Upload PnP PowerShell module to Azure Function
Download PnP PowerShell Module
On local drive create temporary folder
Run the below PowerShell
- Save-Module -Name SharePointPnPPowerShellOnline -Path [pathtoyourfolder]
This will download PnP PowerShell module files to local folder
Upload PnP PowerShell Module to Azure function
- In the Function app, click Platform features
- Under Development tools, select Advanced tools (Kudu)
- On Kudu page, under Debug console select either CMD or PowerShell
- Navigate to site\wwwroot\[nameofyourazurefunction]
- Click New Folder option
- Create folder named modules
- Inside modules folder, create another folder called SharePointPnPPowerShellOnline
- Open SharePointPnPPowerShellOnline folder and drag drop the downloaded PnP PowerShell Module files.
Implement Azure Function
- Open Azure function
- Click View files
- Click Upload
- Upload FlowSiteDesignTemplate.xml
- Update PowerShell as below
- $in = Get-Content $triggerInput -Raw
- Write-Output "PowerShell script processed queue message '$in'"
-
- Connect-PnPOnline -AppId $env:SPO_AppId -AppSecret $env:SPO_AppSecret -Url $in
- Write-Output "Connected to site"
- Apply-PnPProvisioningTemplate -Path D:\home\site\wwwroot\ApplyPnPProvisioningTemplate\FlowDemoTemplate.xml
- Write-Output "Finished provisioning to site"
- To set the environment variables, select Application Settings
- Click Add new setting
- Add the below settings
- Click Save
Step 7 - Create Site Script
Site script JSON will look like below. Refer to the attached site-script-flow.json
- {
- "$schema": "schema.json",
- "actions": [
- {
- "verb": "triggerFlow",
- "url": "[paste the workflow trigger URL here]",
- "name": "Apply Template",
- "parameters": {
- "event":"",
- "product":""
- }
- }
- ],
- "bindata": {},
- "version": 1
- }
Open PowerShell.
Run the below commands
- Connect-SPOService -Url "https://[tenant]-admin.sharepoint.com"
- Get-Content .\site-script-flow.json -Raw | Add-SPOSiteScript -Title "PnP Flow Site Script"
Note down Script ID to register with Site Design
Step 8 - Create Site Design
Open PowerShell.
Run the below commands
- Add-SPOSiteDesign -Title "Site with flow" -SiteScripts [ID of the Site Script] -WebTemplate "64"
Step 9 - Verify the results
To test the entire implementation,
- Create new site in SharePoint tenant.
- Click Team site
- Select design Site with flow
- Fill in the site name
- Click Next
- Observe site design getting applied to site
- Click View updated site.
- Verify Run history of MS Flow is succeded.
- Open Azure queue and see the message in the queue
- Look at the SharePoint site. It should have provisioning changes applied. For example HomePnP.aspx, in this case in "Site Pages" library.
Summary
Site designs can be extended using MS Flow to perform the actions not supported natively by Site scripts and overcome the 30 cumulative actions limitation.