Calling Microsoft Flow From A Site Script

In the previous article, we have gone through the basics of site design and site scripts. In this article, we are going to capture the site creation details like site name, site owner and other site information in the list.
Site design is one of the powerful ways to create a site using the custom templates with a different look and feel from the site collection. The script actions –triggerFlow can be used to call custom solutions and we can track the site creations details.
Step 1 - Create a site directory list
Create a list which will be used to record all sites created using Site design. We have created  list with the name “Site Directory”  and we have the following fields…
  • webUrl (Hyperlink or Picture)
  • webDescription (Single line of text)
  • creatorName (Single line of text)
  • creatorEmail (Single line of text)
  • createdTimeUTC (Single line of text)
Calling Microsoft Flow From A Site Script
Step 2 - Create a Flow
Create an MS Flow which can be referenced in the site design. Create a empty Flow and choose search hundreds of connectors and triggers. Search for Request  and  choose Request-When an HTTP Request is received.
Enter the following 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.       "webDescription": {  
  19.           "type""string"  
  20.       },  
  21.       "creatorName": {  
  22.           "type""string"  
  23.       },  
  24.       "creatorEmail": {  
  25.           "type""string"  
  26.       },  
  27.       "createdTimeUTC": {  
  28.           "type""string"  
  29.       }  
  30.   }  
  31. }  
Select + New Step and  choose Add an action. Search for Create item and Sharepoint-Create item. 
We can select the actions according to your requirement. Here we have selected the Create item actions and followed this by the Send an email notification to the specific user depending upon the requirenment. For each field in the list form, add the corresponding element from the Dynamic Content picker. When we done with the actions, it will look like this....
Calling Microsoft Flow From A Site Script
Choose Save Flow. The Flow will generate the HTTP POST URL that we need to copy for site script triggerFlow action. Choose the first action When a Http request is created  and copy the POST URLand Save Flow. We need that url in comming steps.
Now the flow will look like this.... 
Calling Microsoft Flow From A Site Script
Step 3 - Create the Site Design
Connect to Tenant using Connect-SPOService,
  1. Connect-SPOService -Url https://[yourtenant]  
Step 4 - Now we will get the exisiting site designs
  1. Get-SPOSiteDesign   
To create a Site design, first, we need to create a Site script.
Create JSON code and set the URL property to the value that we copied when we created the flow in Step 2. The URL looks similar to the following...
  1. {  
  2.      "$schema""schema.json",  
  3.      "actions": [  
  4.      {  
  5.              "verb""triggerFlow",  
  6.              "url""",  
  7.              "name""Record site creation event",  
  8.              "parameters": {  
  9.                  "event":"site creation",  
  10.                  "product":"SharePoint Online"  
  11.              }  
  12.          }  
  13.      ]  
  14.  }  
Step 5
Copy the JSON and in the Powershell to copy the script into variable "$site_script" with the following command.
Please save the JSON file in your local path and make sure command prompt pointed to the same. 
  1. $site_script = Get-Clipboard -Raw  
  2. Add-SPOSiteScript -Title "Site Script to record site creation event" -Content $site_scrip  
  3. Get-SPOSiteScript  
Select the ID of the site script that we created, and copy it to the clipboard. 
Step 6
Use the followind command to create the Site Design and here we have used WebTemplate ID 64 which is for the Team site creation with the custom template or we can specify the 68 for Communication site. In this article, we will choose Communication site.
  1. Add-SPOSiteDesign -Title "Record site creation" -Description "The creation of this site will be recorded in the site directory list" -SiteScripts [Paste the ID of the Site Script here] -WebTemplate "68"   
Now the Site Design is created successfully and we can see a new ID is generated for the Site Design.
Now, we will test the flow is working as expected or not, in Site creation. In site creation process, it will trigger an email notification and site details will be stored on our targeted site collection while creating the Flow.
The below screenshot captured the Site creation details in the list Site Directory
I hope you learned something new in this article.