Monday, July 15, 2019

Teamify Group with SharePoint Site Designs and Microsoft Flow - Part 2 - The Flow

In the previous post we looked at the registration of an Azure App:

https://devjhorst.blogspot.com/2019/06/teamify-group-with-sharepoint-site-designs-part-1.html

In this post, I will continue with steps about the Flow creation.


2. Create the Microsoft Flow

When I started writing this blog post, I first had in mind to teamify the group using an Azure Function. But then I started creating the Flow and noticed that the Azure Function was too oversized for this task and Microsoft Flow already supports all the tools needed to realize this kind of task. It means that using Microsoft Flow you can build complex tasks which you've done in the past using a C# program! #lesscodelessfrustration :)

The Flow for this project isn't complicated to create, although its functionality is very powerful. In a first step we define the entering point (HTTP trigger), then we set up variables that are needed for authentication against Azure AD. A further step splits the Office 365 group URL in order for us to be able to obtain the group's mailNickname. This part is very important since the Site Design call to Flow doesn't send much information about the origin group. The mailNickname will be used to search for a group, so we can get the group's id which plays a key role in this logic. The search's response will be then parsed for a better overview. The last HTTP call converts the existing Office 365 group into a Microsoft Team. A pinch of salt and a dash of pepper and the Flow is ready to be served :) The picture below shows the entire process without the salt and pepper stuff:


The picture below shows the finished Flow:


Below, I demonstrate step by step how to create the Flow above:

A HTTP trigger starts the Flow. In the JSON schema we must enter the properties supported by the Site Design triggerFlow action. Here is what it looks like:


The schema payload for the Site Design's triggerFlow action below defines the incoming request object. Here is how it looks:

The next step consists of defining the variables for the OAuth authentication via Azure Active Directory and retrieving the Office 365 Group's mailNickname from the webUrl property. The table below shows the variables used:

VariableDescription
TenantIdYou can find the TenantID in Azure under Azure Active Directory > Manage > Properties > Directory ID
GraphAppClientIDThe client ID of the app just registered
GraphAppClientSecretThe client secret of the app just registered
MailNicknameThis function extracts the mailNickname from the webUrl property last(split(triggerBody()?['webUrl'], '/'))

Here is how the defined variables look:


After defining the variables, it is time to configure a HTTP request to retrieve the group ID which we need to teamify our group. The table below shows the metadata we need in our HTTP request:

VariableDescription
MethodGET
URIhttps://graph.microsoft.com/v1.0/groups?$filter=mailNickname eq '@{variables('MailNickname')}'&$top=1
AuthenticationActiveDirectoryOAuth
Authorityhttps://login.microsoft.com/
Tenant@variables('TenantId')
Audiencehttps://graph.microsoft.com/
Client ID@variables('GraphAppClientId')
Credential TypeSecret
Secret@variables('GraphAppClientSecret')

Since I don't expect to have more than one group with the same mailNickname, I reduced the response number to one by setting the OData query parameter top=1. The authentication process is as simple as what you are seeing in the table above. Thanks to the Active Directory OAuth option, there is no need to request the application access token and build complex calls with it. The picture below shows the "Get group" http request:


In the next step, the Get group's response, which is represented by the Get Group's variable body, will be parsed using the Parse Json action. This way we can easily access the response's content through variables. This action requires a schema which represents the structure of the JSON response. Flow helps you in quickly and easily defining this schema since it automatically creates it for you based on the json payload you fill in. In my sample, I used the JSON representation which in the Microsoft Graph documentation about groups. The picture below demonstrates where you can enter the definition for the outcoming json response.


After entering the Json payload and clicking on Done, your action should look like this:


The last step executes the HTTP request which converts the Office 365 Group into a Microsoft Team. Since we already have all the metadata required to execute the call, this step will be short. A special aspect is the URI! Although we limited the Get Group's response to only one Office 365 Group, the Graph API returns a collection of Groups. Therefore, I used the method first combined with the Parse JSON's variable value to get the ID of the first Group in this collection. The table below shows the metadata we need in our HTTP request. Note that the authentication part didn't change:

VariableDescription
MethodPUT
URIhttps://graph.microsoft.com/v1.0/groups/@{first(body('Parse_JSON')?['value'])?['id']}/team
AuthenticationActiveDirectoryOAuth
Authorityhttps://login.microsoft.com/
Tenant@variables('TenantId')
Audiencehttps://graph.microsoft.com/
Client ID@variables('GraphAppClientId')
Credential TypeSecret
Secret@variables('GraphAppClientSecret')

 The picture below shows the "Teamify group" HTTP request:


Saving the Flow generates the trigger method's URL. Note this URL since we will need it in the next step!


You'll find the steps below in the third and last part of this article:

• Create the Site Script
• Create the Site Design
• Apply the Site Design

Teamify Group with SharePoint Site Designs and Microsoft Flow - Part 3 - The Site Design

To be continued...

No comments:

Post a Comment