Extend Site Designs With Microsoft Flow In SharePoint Online

The Modern site offers two out of box templates - Team Site and Collaboration Site. These templates can be extended by using Site Designs to provision additional SharePoint artifacts on the site.

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
  1. Open SharePoint tile (https://tenant.sharepoint.com/_layouts/15/sharepoint.aspx)
  2. It offers an option to create a new Modern SharePoint site.
  3. The available site designs (out of box and custom) appears in site creation process.

    SharePoint

  4. After selection of site design, providing site name and option site description, click Finish.
  5. 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.
    SharePoint

  6. After the site scripts have finished running, we will see the updates to the site.
Limitation of Site Scripts
  1. The actions in site script are limited and do not fit all possible scenarios, for example adding footer to every page or creating pages.

  2. 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.
  1. {  
  2.      "$schema": "schema.json",  
  3.      "actions": [  
  4.      {  
  5.              "verb": "triggerFlow",  
  6.              "url": "[paste the workflow trigger URL here]",  
  7.              "name": "Apply Template",  
  8.              "parameters": {  
  9.                  "event":"",  
  10.                  "product":""  
  11.              }  
  12.      }  
  13.      ],  
  14.      "bindata": {},  
  15.      "version": 1  
  16.  }  
Site Design Extensibility Process

The below diagram explains the process to extend the site design using Microsoft flow.

SharePoint
 
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

  1. Open SharePoint root site. You may also perform this on any other SharePoint site other than root site
  2. Navigate to url https://[tenant].sharepoint.com/_layouts/15/appregnew.aspx
  3. Generate new Client Id and Client Secret by clicking Generate button next to it.
  4. Enter title for app. Example: "Extend Site Design"
  5. In the App Domain, specify localhost
  6. In the redirect URI, specify https://localhost
    SharePoint 

  7. Click Create

Note down the Client ID and Client Secret for future reference.

Trust the App for appropriate access to tenant

  1. Open SharePoint central admin site.
  2. Navigate to https://[tenant]-admin.sharepoint.com/_layouts/appinv.aspx
  3.  Paste the Client Id to App Id text box
  4. Click Lookup
  5. Paste below XML in Permission Request XML textbox
    1. <AppPermissionRequests AllowAppOnlyPolicy="true" >    
    2.       <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />    
    3. </AppPermissionRequests>  
    SharePoint
  6. Click Create
    SharePoint
  7. 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.

  1. Open Azure Portal https://portal.azure.com
  2. Click Create a resource
  3. Select Storage > Storage account - blob, file, table, queue

    SharePoint

  4. Provide the values by filling up the form

    SharePoint

  5. Click Create
  6. Open Storage account, navigate to Queues.

    SharePoint

  7. Create queue with name sitedesignqueue

    SharePoint

  8. Click Access keys under Settings
  9. Note down Storage Account Name and the key1 Key value for future reference.
Step 3 - Setup Microsoft Flow
  1. Open SharePoint site
  2. In the next tab navigate to MS Flow (https://flow.microsoft.com)
  3. Click My flows

    SharePoint

  4. Click Create from blank

    SharePoint

  5. From search results, select Request - When an HTTP Request is received
  6. Search Request
  7. Select Request - When a HTTP Request is received

    SharePoint

  8. Enter below JSON as request body
    1. {    
    2.      "type""object",    
    3.      "properties": {    
    4.          "webUrl": {    
    5.              "type""string"    
    6.          },    
    7.          "parameters": {    
    8.              "type""object",    
    9.              "properties": {    
    10.                  "event": {    
    11.                      "type""string"    
    12.                  },    
    13.                  "product": {    
    14.                      "type""string"    
    15.                  }    
    16.              }    
    17.          }    
    18.      }    
    19. }    
  9. Click New Step 
  10. Select Add an action

    SharePoint

  11. Search Azure Queues
  12. Select Azure Queues - Put a message on a queue

    SharePoint

  13. Enter any name for connection
  14. Enter storage account name
  15. Enter storage shared key

    SharePoint

  16. Click Create
  17. Select the queue name
  18. In the message, select webUrl

    SharePoint

  19. Click Save
  20. At the end, the flow will look like below

    SharePoint

  21. Note down HTTP POST URL for future reference.

Test the flow

Run below PowerShell

  1. $uri = "[Flow URI]"  
  2. $body = "{webUrl:'siteurl'}"  
  3. Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Body $body  

See the run history, if flow is successful.

SharePoint 
Step 4 - Create PnP provisioning template

Connect to SharePoint Modern site using PnP PowerShell

  1. Connect-PnPOnline -Url "SharePoint site url"  
Run the below command to generate the provisioning template from the site
  1. Get-PnPProvisioningTemplate -Out .\CustomTemplate.xml -ExcludeHandlers Navigation, ApplicationLifecycleManagement -IncludeNativePublishingFiles  
Make edits to the xml or use attached FlowSiteDesignTemplate.xml

Step 5 - Setup Azure Function
  1. Open Azure Portal https://portal.azure.com
  2. Click Create a resource
  3. Select Compute > Function App

    SharePoint

  4. In Storage field, select Use existing and select the storage account created earlier.

    SharePoint

  5. Click Create
  6. Open Function app. Select Functions > New function

    SharePoint

  7. From language dropdown, select PowerShell

    SharePoint

  8. Select QueueTrigger - PowerShell
  9. Name function as ApplyPnPProvisioningTemplate
  10. Enter the queue name created earlier

    SharePoint

  11. 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

  1. Save-Module -Name SharePointPnPPowerShellOnline -Path [pathtoyourfolder]  
This will download PnP PowerShell module files to local folder

Upload PnP PowerShell Module to Azure function

  1. In the Function app, click Platform features

    SharePoint

  2. Under Development tools, select Advanced tools (Kudu)

    SharePoint

  3. On Kudu page, under Debug console select either CMD or PowerShell

    SharePoint

  4. Navigate to site\wwwroot\[nameofyourazurefunction]
  5. Click New Folder option

    SharePoint

  6. Create folder named modules
  7. Inside modules folder, create another folder called SharePointPnPPowerShellOnline
  8. Open SharePointPnPPowerShellOnline folder and drag drop the downloaded PnP PowerShell Module files.

    SharePoint 

Implement Azure Function

  1. Open Azure function
  2. Click View files

    SharePoint

  3. Click Upload
  4. Upload FlowSiteDesignTemplate.xml
  5. Update PowerShell as below
    1. $in = Get-Content $triggerInput -Raw    
    2. Write-Output "PowerShell script processed queue message '$in'"    
    3.     
    4. Connect-PnPOnline -AppId $env:SPO_AppId -AppSecret $env:SPO_AppSecret -Url $in    
    5. Write-Output "Connected to site"    
    6. Apply-PnPProvisioningTemplate -Path D:\home\site\wwwroot\ApplyPnPProvisioningTemplate\FlowDemoTemplate.xml    
    7. Write-Output "Finished provisioning to site"   
  6. To set the environment variables, select Application Settings

    SharePoint

  7. Click Add new setting
    SharePoint
  8. Add the below settings
SPO_AppIdValue of Client ID created in Step 1: Setup App Only Access to Office 365 Tenant
SPO_AppSecretValue of Client Secret created in Step 1: Setup App Only Access to Office 365 Tenant

  1. Click Save
Step 7 - Create Site Script

Site script JSON will look like below. Refer to the attached site-script-flow.json

  1. {  
  2.      "$schema""schema.json",  
  3.      "actions": [  
  4.      {  
  5.              "verb""triggerFlow",  
  6.              "url""[paste the workflow trigger URL here]",  
  7.              "name""Apply Template",  
  8.              "parameters": {  
  9.                  "event":"",  
  10.                  "product":""  
  11.              }  
  12.      }  
  13.      ],  
  14.      "bindata": {},  
  15.      "version": 1  
  16.  }  
Open PowerShell.

Run the below commands

  1. Connect-SPOService -Url "https://[tenant]-admin.sharepoint.com"  
  2. 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

  1. Add-SPOSiteDesign -Title "Site with flow" -SiteScripts [ID of the Site Script] -WebTemplate "64"  
Step 9 - Verify the results

To test the entire implementation,

  1. Create new site in SharePoint tenant.
  2. Click Team site
  3. Select design Site with flow

    SharePoint

  4. Fill in the site name
  5. Click Next
  6. Observe site design getting applied to site

    SharePoint
  7. Click View updated site.
  8. Verify Run history of MS Flow is succeded.
    SharePoint

  9. Open Azure queue and see the message in the queue

    SharePoint

  10. 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.