Site icon @WonderLaura

Run Power Automate as Part of a SharePoint Site Design

Back in the old days of SharePoint, you could save a whole site as a template, and any workflows that were on that site, were automatically part of the template.  So, sites that were created from that template then automatically had the workflows included.

Now, “Site Designs” are the new way of creating a sort of site template in SharePoint Online, and Microsoft Flow is the new way of doing workflows.  Flows aren’t really a part of a site like old workflows used to be, so Flows aren’t automatically going to be included.

In site designs, there is a way to make the site design trigger a Flow to run, when the site design is being applied.  This can be done as the site is being created, or later on.

Here’s the scenario.  I have a Flow on a site, that sends out emails about the tasks in the task list on that site.  Pretty simple.  I kept it as simple as possible for testing and figuring this out.  I want that Flow to get created as part of a site design, so that the task list on that site will automatically have that Flow associated with it, and running right when the design is applied.  This ended up being extremely complicated to figure out, especially since I’m not a dev.  It took way longer than I expected.

Check out my Advanced Flow Course

High level:  To try this out, you will have two Flows.  First is the Flow that gets triggered from the site design.  I’m just calling mine “HTTP Trigger”, but in reality you should give it a name that is relevant to the site design and what it does.  The second Flow, like “new task email” or “document approval” will be the one that you want to have a copy of as part of the site template (site design) and all the sites that get created with that design.

This is an overview of the first workflow that creates the second one.  Also, there is an hour long demo video at the end of this post, of me building this.

Here are the steps, and the way I finally got it to work:

1.  In a new, blank Flow,  add the trigger called When a HTTP request is received.

2.  Add an action Initialize variable.  Name the variable varJSON and Type is Object.

3.  Give the Flow a name, and Save the Flow.  This will be your Flow that will be triggered when the site design is applied, which will then create a Flow that is specific to that site.  I’ll call mine “HTTP trigger

4.  You need another Flow.  It doesn’t really matter what it does, like approvals or notifications, but I recommend keeping it fairly simple when you first try this.  I called mine “Scheduled Task Reminder” and it runs every day and looks at a task list in a SharePoint site and sends emails about overdue tasks.  Create this other Flow and make sure it is fully functional, and tested.  Also, remember what site that it’s associated with.  This part in yellow, this is the important thing that you’ll need to remember, the URL to the SharePoint site.  In production, you could just have this as a disabled (turned off) workflow associated with some test site, but it does need to exist, and you don’t want to delete it.

5.  Open the first Flow that you created at step 1.  Copy the HTTP POST URL to the clipboard.

6.  This next step is done in PowerShell.  Here is the script.  It connects to your site, creates a site script that entails the creation of a very simple task list, and it triggers your first Flow.  See the part that says “Your Flow Http Trigger URL”.  That’s where you need to paste the URL that you just copied to the clipboard.  What did you name your first Flow, at step 3?  Put the name of it there below where it says “your first Flow name”.  Run this script.  New to PowerShell?

____________________________

Connect-SPOService -Url https://yourtenant-admin.sharepoint.com

$site_script = @'
  {
   "$schema": "schema.json",
   "actions": [
      {
       "verb": "createSPList",
       "listName": "Tasks",
       "templateType": 100,
       "subactions": [
         {
           "verb": "SetDescription",
           "description": "List of Tasks"
         },
         {
         "verb": "addSiteColumn",
         "internalName": "AssignedTo",
         "addToDefaultView": true
         }       ]
     },
      {
     "verb": "triggerFlow",
     "url": "YOUR FLOW HTTP TRIGGER URL",
     "name": "Your first Flow Name",
     "parameters": {
         "Event": "testevent",
         "Product": "testproduct"
         }
     }   ],
   "bindata": { },
   "version": 1
}
'@
$newscript = Add-SPOSiteScript -Title "Tasks and Flow" -Content $site_script -Description "Creates tasks with a Flow in a site"

Add-SPOSiteDesign -Title "Tasks with Flow" -WebTemplate "64" -SiteScripts $newscript.Id -Description "Has tasks and a flow"

Check out the Microsoft Flow class that I teach!
7.  Test it.  Go to any of your SharePoint sites, or create a new one.  Go to the Settings gear, and click Site designs.

8.  The one you just created is called “Tasks with Flow”.  Pick that from the drop-down, and choose Apply to site.

9.  In Flow, go to your first workflow, and after a couple of minutes (maybe up to 5), you should see “Succeeded” in the run history.  Edit the Flow again.

10.  Add a new step, the action called Parse JSON.

For the Content, select the Body from the HTTP request.

For the Schema, paste this:

{
"type": "object",
"properties": {
"webUrl": {
"type": "string"

},
"webTitle": {
"type": "string"

},
"webDescription": {
"type": "string"

},
"groupId": {
"type": "string"

},
"creatorEmail": {
"type": "string"

},
"creatorName": {
"type": "string"

},
"createdTimeUTC": {
"type": "string"

},
"parameters": {
"type": "object",
"properties": {
"Event": {
"type": "string"

},
"Product": {
"type": "string"

}}}}}

11.  Next, add the action called Get Flow.

12.  Select your environment, and then for the Flow, pick the Flow that you decided upon at step 4 above.  The one I’m using is called Scheduled Task reminder.

13.  Add the action called Compose. Basically, we need to find the old SharePoint site URL from the flow we got at step 12, and replace it with the URL of this new site.  It has to be a string in order to do the replace function, so this is the best way I could figure out.

Put the Flow Definition in there as the Input, and put single quotes on either side of it.  That converts it from a JSON object to a string.  I renamed my action instead of Compose, Flow definition JSON to string

14.  Add the action called Compose again. For the Input, use this expression.

replace(outputs('Flow_definition_JSON_to_string'),'https://yourtenant.sharepoint.com/yoursite',body('Parse_JSON')?['webUrl'])

Remember at step 4 when you created that second Flow, and remember what the URL is to that site (highlighted in yellow at step 4)?  This is where you’ll need that.  Notice the single quotes around the URL.  Also notice that it’s got the exact names of my actions in the formula here, so if you name your actions differently that what mine are, your formula will need to be adjusted as well.  To see an overview of what I named all my actions, refer to the screenshot at the very beginning of this post.  Also, that webUrl is what comes from parsing the JSON at the beginning, where it got all of the info about this new site, like it’s name and URL.  I renamed my action here instead of Compose, Find and replace in string

15.  Add the action Set Variable.  For Name, select the name of the variable that you created at step 2, varJSON.

For the Value, use this expression:

json(substring(outputs('Find_and_replace_in_string'),1,sub(length(outputs('Find_and_replace_in_string')),2)))

16.  Finally, add the action called Create Flow.  Pick your own environment, and for the Flow display name, I recommend something with the name of the site, and what the workflow does.  So I call mine Task reminder – (and then put the name of the site in there).  For the Flow Definition, use the varJSON variable.  Flow state is up to you.  I set mine to Started.

17.  Connection references.  This is the part that I got stuck on in my video demo.  I never did get it figured out, and then figured it out the next day (today).   Click the little icon circled here.  It’s called Switch to input entire array.

18.  After you’ve clicked the icon, the connection references field changes to a single field instead of two.  Insert the connectionReferences from your Get Flow action, like this:

19.  Save the workflow. Done!  Since you’ve already run the workflow back at step 7, you can simply re-run it.  While editing the Flow, click the Test button at the top right, and choose Using data from previous runs.  Click the first one in the list, and click Save and Test.

After the Flow runs, this time it will create another Flow.  So when you go back to your list of Flows and refresh it, the new one will be there, with the name you gave it at step 18.  To see me build this whole thing, and all of my troubleshooting, here is the video:

Here are some good references related to the topics in this post:

THE ULTIMATE GUIDE TO SHAREPOINT SITE DESIGNS AND SITE SCRIPTS

Get started creating site designs and site scripts

Site design JSON schema

Check out the Microsoft Flow class that I teach!


Exit mobile version