Create a new Team from Microsoft Flow

Steven Collier [MVP]
REgarding 365
Published in
5 min readJan 7, 2019
Photo by Mike Lewis HeadSmart Media on Unsplash

This is the first in a series of blogs I’m preparing about automating the provisioning of Microsoft Teams. Creating a new site to a specific template is something that’s long been part of the SharePoint world and is common in many organisations. Users get a pre-configured site that’s been tuned by their organisations to be ready to use and also consistent with other sites. For example, in a sales process a ‘bid site’ might be a team site with a specific set of document libraries, permissions, default documents and so forth.

Now, as Teams is the new ‘hub for teamwork’ and it’s being adopted widely by business people want one place for their conversations, meetings, files and applications so that same ‘bid team’ might have specific channels, tabs and files. Furthermore, these bid sites might need to be tracked through some master list for people to find in the future.

Beyond Citizen Developers …

Microsoft Flow is a fantastic tool to pull together elements of Office 365, through a wide range of triggers and actions it’s pretty easy to construct workflows. There is a connector for Microsoft Teams with the following functions …

That’s quite a short list, and critically there isn’t an action to create a new team. So at this step we start to get a little beyond the casual user building a flow, as the tools get a little more developer-y beyond this point. Microsoft provide a PowerShell module for Teams, current version is 0.96 and available here with documentation.

Teams PowerShell provides a command for a New-Team, this can either create something entirely new, or if you tell it about an existing group it can add teams functionality to the group. Typically you would write a script on commands in PowerShell and run them as a suitable admin to create your solution, but we want that to be triggered from Microsoft Flow so …

In to Azure Automation …

Azure Automation is built exactly for this purpose, load your PowerShell scripts into the service, then from Flow you can use the Create Job card to trigger that script and pass your values to the script. As an Azure service this isn’t included in your Office 365 licensing, but Azure Automation will only start costing after your scripts have been running for 500 minutes per month. In my testing creating a Team takes about 30 seconds, so you could create 1000 Teams a month before costs kick in (at a negligible cost of $0.001 per minute). The challenge in a business environment is in creating that subscription, linking it to a credit card or into your Microsoft account. If you are using Azure elsewhere find out if they can add you to an existing subscription, you probably don’t want to use a personal credit card then find out someone else decided to run some super expensive services against it.

Setting up the Azure Automation service

1. First you will need a resource group, ideally this should be in the same location as your Office 365 tenant. It doesn’t really matter, I created my first one in Japan by mistake it just added a few seconds on to each run.

2. Now create an Azure Automation account, this will be for all your Teams provisioning scripts so name it something useful to remember.

3. We will need to make the Microsoft Teams PowerShell module available to our scripts, so in your automation account find Teams in the module gallery and then choose to Import.

4. We’re also going to need some credentials in order to log into Teams PowerShell, it’s best to not have these in your scripts, so if you go ahead and add a credential for ‘TeamsAdmin’ under the Credential section of shared resources.

The easy bit

Writing the actual runbook is the easiest bit, it’s only got to connect to Teams PowerShell and run one command, so create a new runbook of type PowerShell and start editing, you can do it all in the browser.

We start our script by defining the parameters we are going to pass in from Flow, these get displayed in the Create Job card for you to add your values. For example, you could trigger your Flow from a SharePoint list and pass in fields to the script.

That couldn’t have been much easier now could it? Go ahead and test it in Automation using the Test Pane. It’ll take a little while for your test to start (tests get lower priority than real jobs). In the return window you should see an output showing your new Teams like …

And before you go, remember to press publish on the Runbook, I can’t tell you how many times I’ve omitted this step and spent ages figuring out why my code doesn’t work.

Back to Flow

All our elite development is now over, we’re back to being a normal user in Flow now to compete the task. In my example I’m triggering the Flow from a SharePoint list entry being added, but you might choose to also use a Microsoft Form, and any number of the other connectors.

So calling my Runbook is as easy as

At the end of that process my flow can continue and the Parse JSON block exposes the GroupID of the new Team. I could pass that into the standard flow command to add channels to my Team.

This pattern would allow you to use any of the Teams PowerShell commands to control your Team, so for example you could also add members, configure Team settings and so on. It was if you like your warm-up lap, you’ll notice fairly soon that currently Team PowerShell is also fairly limited, in the next article in the series we take it a step further and start calling the Microsoft Graph directly for far more interesting effects.

--

--