Creating Microsoft Teams from Template using Microsoft Graph API in Power Automate

Introduction: In this article, we will guide you through the process of creating Microsoft Teams from a template using the Microsoft Graph API in Power Automate.

Prerequisites:

  1. Teams created in Microsoft Teams which acts a Template
  2. Access to Azure AD to register an app.
  3. Access to the Power Automate maker portal.

Implementation Steps:

  1. Register an application in Azure AD and grant necessary Teams/Group permissions.
  2. Build a flow in Power Automate that performs the following actions:
  • Retrieve the Template Teams based on the template name
  • Send a Graph API request to create new Teams by cloning the Template Teams
  • Check the Teams provisioning status to ensure the Teams are successfully created.

Step 1: Register an application in Azure AD and add Teams/Group Permission

  1. Go to Azure AD
  2. Select App Registration > New Registration > Fill out the form

3. Copy Client ID and Tenant ID for later use

4. Generate Secret: Certificates & Secrets > New Client Secret > Fill Description > Add

5. Copy Secret (It won’t be visible later. So, note down for later use)

6. Select target API to add Permission: API Permissions > Add a Permission > Microsoft Graph

7. Add Permission: Application Permissions > (scroll down to Groups)Select Group.ReadWrite.All > Add Permissions

8. Grant Consent: Click ‘Grant admin consent for <tenant ID>’ (Status should turn to green)

Step 2: Build flow in Power Automate

  1. Go to Power Automate maker portal : Go to Microsoft365 portal > Select Power Automate

2. Click New flow > Instant cloud flow

  • Note : In automation scenarios, we need to use ‘Automated cloud flow’ with relevant trigger

3. Enter Flow name, Select Manually trigger a flow > Click Create

4. Create Trigger Input Parameters: Add Input

5. Add 2 input properties: Select Text(Input Type) > fill name and leave blank value

  • Teams Template Name
  • New Teams Name

6. Click New Step > Initialize variable (select from list of actions) > fill details. (Repeat this step and add variables as described in screenshot)

7. Click New Step > select Http > fill details and rename it as ‘GET Template Teams ID’

"method": "GET",
"uri": "https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '@{triggerBody()['text']}'&$select=id",
"authentication": {
"type": "ActiveDirectoryOAuth",
"authority": "@variables('varAuthorityUrl')",
"tenant": "@variables('varTenantId')",
"audience": "@variables('varGraphApiAudience')",
"clientId": "@variables('varClientID')",
"secret": "@variables('varClientSecret')"
}

8. Run and verify: Click Test (option exist at top right corner) > Manually > Test > Fill Details > Click Run flow > Done

Teams Template Name : Explore-Graph-API (Name of your Template Teams. This must have created as part of pre-requisites )
New Teams Name : Project-001 (or as you wish)

9. Succeeded Test run looks as below

10. Click GET Template Teams ID and Copy response body (which is required for next step)

11. Continue to edit the flow : Click Edit (option exist at top right corner)

12. Click New Step > parse JSON (select from list of actions) > Fill details and Rename it as ‘Parse JSON response of GET Template Teams ID’

  • Content — Body of ‘GET Template Teams ID’
  • Schema — Click ‘Generate from Sample’ > Paste the value which copied from previous step > Done

13. Click New Step > Compose (select from list of actions) > Fill Input field by pasting below formula in Expression textbox > Rename it as ‘Compose Template Teams ID’

body('Parse_JSON_response_of_GET_Template_Teams_ID')?['value'][0]['id']

14. Clone Teams: Click New Step > Http (select from list of action) > fill details and Rename it as ‘Create Teams’

"method": "POST",
"uri": "https://graph.microsoft.com/v1.0/teams/@{outputs('Compose_Template_Teams_ID')}/clone",
"body": {
"displayName": "@triggerBody()['text_1']",
"description": "created by cloning @{triggerBody()['text']}",
"mailNickname": "@triggerBody()['text_1']",
"partsToClone": "apps,tabs,settings,channels,members"
},
"authentication": {
"type": "ActiveDirectoryOAuth",
"authority": "@variables('varAuthorityUrl')",
"tenant": "@variables('varTenantId')",
"audience": "@variables('varGraphApiAudience')",
"clientId": "@variables('varClientID')",
"secret": "@variables('varClientSecret')"
}
  • NOTE: Clone request returns Response code 202, means request accepted. Provisioning might take more time depends on size of Template Teams. So, write more logic to check the provisioning status

15. Click New Step > Compose (select from list of actions) > Fill Input field by pasting below formula in Expression textbox > Rename it as ‘Compose URL to get Teams provision status’

concat('https://graph.microsoft.com/v1.0',outputs('Create_Teams')['headers']?['Location'])

16. Click New Step > Initialize Variable (select from list of actions) > Fill details as mentioned below and Rename it as ‘Initialize Teams Provisioning Status’

  • Name : varTeamsProvisioningStatus
  • Type : String
  • Value : inProgress

17. Click New Step > Do until (select from list of actions) > Fill details and Rename it as ‘Do until Teams Provisioning Status to Succeeded’

Condition :- ‘varTeamsProvisioningStatus’ is equal to ‘succeeded’

18. Add action inside Do until block: Click Add an Action > select Http > fill details and Rename it as ‘Get Teams provisioning status’

"method": "GET",
"uri": "@{outputs('Compose_URL_to_get_Teams_provision_status')}",
"authentication": {
"type": "ActiveDirectoryOAuth",
"authority": "@variables('varAuthorityUrl')",
"tenant": "@variables('varTenantId')",
"audience": "@variables('varGraphApiAudience')",
"clientId": "@variables('varClientID')",
"secret": "@variables('varClientSecret')"
}

19. Add action inside Do until block: Click Add an Action > select ‘Set Variable’ and Rename it as ‘Set Teams Provisioning status’ > Fill details as below and Click OK

Name: varTeamsProvisioningStatus
Value: body('Get_Teams_provisioning_status')?['status']

20. Add action inside Do until block: Click Add an Action > select ‘Condition’ and Rename it as ‘Check Teams Provisioning Status’

  • Condition : ‘varTeamsProvisioningStatus’ is equal to ‘inProgress’
  • If Yes : Delay for 1 Minute
  • If No : no action

21. After adding the actions inside Do until block, it looks like this

22. Run and verify: Click Test (option exist at top right corner) > Manually > Save & Test > Fill Details > Click Run flow > Done

  • Teams Template Name : Explore-Graph-API
  • New Teams Name : Project-001

23. Verify your Teams

Things to consider:

It is allowed to create more Teams with same name. During the automation, check the exitance of teams with the display name before creating new Teams

Graph endpoint to check: https://graph.microsoft.com/v1.0/groups?$filter=displayName eq ‘new-teams-name’

Conclusion:

By following the above steps, you will be able to automate the creation of Microsoft Teams from a template using the Microsoft Graph API in Power Automate. This approach provides an efficient way to provision Teams based on predefined templates, saving time and effort in the process.

Comments

Popular posts from this blog

Integrating ServiceNow with Power Automate via REST API

How to Check Null Values in Power Automate Filter Query

Validate Email Format in Power Automate Using Custom Connector and Regex in C#