Run a Flow 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.

image

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.

recurring-flow

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

http-request-flow

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.

settings-site-designs-sharepoint

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

tasks-with-flow-site-design

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.

flow-parse-json

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.

get-flow-action

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

flow-definition-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

find-replace-string-flow

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.

create-flow-action

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:

create-flow-connection-references

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.

test-microsoft-flow

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:

2019-03-14_19-20-12

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!

6 comments

  • I don’t see Site Designs in my Office 365 SharePoint settings. Do I need to turn on something? I am a global admin.

    Like

  • Maurice Bakker

    Great post and nice to see this can really work, but is this really a good solution? If you have less than 30 sites it is quicker to manually add the flows, and if you have more what would your Flow overview (My Flows, or Team Flows) look like: 30+Flows with the same name, I presume. And what if the process needs to be changed and the logic is copied this many times in seperate Flow instances… Isn’t it better to wait until Flow comes with a “reusable” Flow solution, or to use the business flows?

    Like

    • Sure, you can wait until Microsoft comes up with an official solution, but this is the only way to accomplish it as of now. You’re definitely right that it’s messy.

      Like

  • Laura – thanks for the detailed instructions. I have used this as a guide to create a site design that runs a flow, and it works up to a point. The Get Flow step in the HTTP Trigger Flow returns a flow definition that contains a reference to the url of the original site where the flow was created (this is what we replace in the Find and Replace in String Step). The flow definition also contains a reference to the GUID of the list (not the list name) on the original site. When I create a new site with this site design, a new list is created which has a GUID different than that contained in the flow definition, so the flow that was created by the site design fails because it can’t find a list on the new site that has a GUID that matches the list GUID contained in the flow definition. If the flow definition contained the list name rather than the list GUID, it seems like this would work, but the different GUIDs is making it fail. Did you run into this issue?

    Like

    • In mine, I had it creating the list called Tasks as part of the site design. I was able to simply use the word “tasks” in my workflow, and it worked without a GUID. Try another test, using a very short simple list name, no spaces, and see if it will let you use that instead of a GUID.

      Like

Leave a Reply to Sandy Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.