cancel
Showing results for 
Search instead for 
Did you mean: 
anttipajunen

Creating a Team, a channel and a tab with D365 CE in Microsoft Teams using Flow

This blog post is about enabling collaboration with Microsoft Teams when a Project is created in Dynamics 365 Project Service Automation. The Flow covered on this post will check whether an Account already has a Teams team or not. If a team is present, a new channel for a Project and a PSA tab are created. If a team is not present, the Flow does a bit more and creates an Office 365 Group, a team, a channel and a PSA tab. I will update this post at a later date for scenarios around the new Project service, when it is generally available. But for now, let’s dissect the Flow!

 

The initial steps of the Flow

 

The Flow fires off when a Project is created and the msdyn_customer field contains data. This way a team and/or a channel is only created when an Account is related to a Project. An Account Name is needed later on when creating a team so a get action for accounts is needed. A user is needed when a group is created later on in the Flow. I’ve included both the Office 365 Users action and the Azure AD users action for educational purposes as the outputs of the actions differ slightly.

Flow-1-initial-steps.png

 

As we move down the Flow, we will hit an action that requires the URL of the environment we are using. There are a few ways to get URL and the initial scope (Demo Scope – indexOf and substring for environment id) is for educational purposes. It shows an alternative approach to getting the URL of an environment.

Flow-2-demo_scope.png

 

The next scope is full of compose actions. The first two are used to get the environment’s URL both with and without https://. The Get User record action’s output conveniently contains an environment URL. Using first, last, split in an expression gets the URL with just a single compose. The expressions in the first two compose actions are:
first(split(last(split(body(‘Get_User_record’)?[‘@odata.id’],”)),’/api’))
first(split(last(split(body(‘Get_User_record’)?[‘@odata.id’], ‘https://’)), ‘/api’))

Flow-3-environment_url.png

 

Next, we need the environment id. That is also needed down the line when a tab with PSA is added to a channel. Composing the URL and the id of the environment makes Application Lifecycle Management easier as the values don’t need to be entered manually, when moving the Flow in a solution from development to test and then to production. The expression used in the compose is:
body(‘Get_User_record’)?[‘organizationid’]


Flow-4-environment_id.png

 

The values for the next compose actions require a bit of research. Some values are unique to an environment so be sure the change them, when moving the Flow between environments.
  • App id: The D365 CE or PSA app id is easy to find by opening the model-driven app in question and copying the app id from the URL. This value is unique to an environment.
  • View id and view name: The id for a view that you want to display in the app on the tab is also unique to an environment. An OOTB view called All Projects is used.
  • Channel name: The channel’s name equals the Project’s name.
  • teamsAppId for Dynamics 365: This value only needs to be extracted once as it’s not unique to an environment. The value can be extracted with Graph Explorer and a HTTP GET request. Image 6 illustrates the request’s results. The request used is https://graph.microsoft.com/v1.0/appCatalogs/teamsApps. See this article by Microsoft for more information.
  • Compose body: Compose JSON. Used later on when adding the D365 CE app to a team. The dynamic value used in the JSON is from the previous compose, that holds the teamsAppId.

Flow-5-compose_actions.png

Flow-6-graph_explorer.png

 

Next, all teams are listed and a condition checks whether a team exists for the account that is related to the Project. For the condition to work, the string version for the value in needed. If the body of the list includes the name of the account, the condition is true and only a channel and tab with PSA are created. If the condition is false, a group, a team, a channel and a tab with PSA are created. The expression used in the condition is:
string(body(‘List_Teams_to_see_if_the_Account_has_a_Team’))


Flow-7-condition.png

 

The true side of the condition
 

To create a channel, a team id is needed. To get our hands on it, a filter array is used and the id is then composed in the apply to each. The name of the channel will be based on the Project’s name. Let’s look at an error handling step for duplicate channel names next.


Flow-8-true_side_filter_array.png

 

If a channel matching a Project’s name already exists, a new channel can’t be created. Instead of just canceling the Flow, an email notification will be sent. The notification will run after the Create a Channel on yes side has failed.

 

When a channel is created, a compose action will capture its id. An HTTP POST action will then be used to add the Dynamics 365 app to the existing team. If the app has already been added, the action will fail. This is why the delay that follows runs both when the HTTP POST has succeeded and failed. The delay of 15 seconds is something that you can play around with to see if the delay can be smaller or even non existent. I’ve added it for a bit of extra safety.


Flow-9-error_handling-and-post.png

 

The next compose JSON is where all the initial compose actions come into play. Adding the D365 app to a channel’s tab required quite a bit of “trial and error”. Kudos to my colleague MVP Vesa Nopanen for helping me figure out the JSON! The easy way out would be to simply skip all the dynamic values but my goal was to make it easy to move the Flow from one environment to another. The image below illustrates all the outputs of the initial compose actions.


Flow-10-JSON_add_app_to_tab.png

 

The next action on the true side of the condition is to add a tab with D365/PSA to a channel. Note that at the time of writing this, adding a tab only works with Microsoft Graph API Beta! After this, there is one final thing that needs to be done and that is to add the user firing off the Flow to the account’s Office 365 Group. This can be placed earlier in the Flow. I’ve put it at the end because I forgot to add the actions earlier. To get the id of a group, an HTTP GET is needed. The filter that is being used is:
https://graph.microsoft.com/v1.0/groups?$filter=mailNickname eq ‘ replace(body(‘Get_Account_record’)?[‘name’], ‘ ‘, ”) ‘
 

Next, the JSON from the HTTP GET is parsed. The schema used can be found here. Using a compose for a group’s id throws an apply to each but there should only be a single value that is returned. Based on the group id, the user can be added to the group. These actions wrap up the Flow on the true side. Next, let’s dissect the false side.

Dynamic content for the get action has been replaced with a replace expression. A mailNickname can’t have spaces. If an account’s name has them, its mailNickname is nevertheless composed without any spaces in it.

Flow-11-POST_tab.png


Flow-11-1-add-to-group-updated_image.png

 

If a user already belongs to the group in question, the Add user to group on yes side action will fail. To display the Flow as successful, a terminate action can be placed after the apply to each (terminate actions can’t be inside a loop). Configure run after for the action has been set so that the terminate action only runs after the apply to each has failed.

11-2-Terminate-after-apply-to-each.png

 

The false side of the condition
 

The first action on the false side is to compose a JSON to create a Teams team. The community is full of JSON examples for creating a team so replace the JSON with something that fits your specific requirements. After the compose, an Office 365 Group is created.

{
“memberSettings”: {
“allowCreateUpdateChannels”: true
},
“messagingSettings”: {
“allowUserEditMessages”: true,
“allowUserDeleteMessages”: true
},
“funSettings”: {
“allowGiphy”: true,
“giphyContentRating”: “strict”
}
}


The action that crates a group has an incorrect dynamic value in the Mail Nickname field. If an account consist of a name with a space (for example Beer Company), the Flow will fail. The value Mail Nickname can’t have any spaces. A replace expression can be used to concatenate the string:
replace(body(‘Get_Account_record’)?[‘name’], ‘ ‘, ”)

Flow-12-JSON_for_team.png

 

If the creation of a group fails, an error handling step sends a notification email. Group creation can fail if multiple Projects for the same Account are created simultaneously. Concurrency Control for the Flow’s trigger can be adjusted to compensate for this.

Flow-13-trigger_concurrency_control.png

 

When a group has been created, a compose stores its id. The next action is to add the user firing off the Flow to the created group. The JSON that was composed (image 12) will be used in the body of the HTTP PUT action. This creates a new team in the group. The delay is again something you can play around with to see if a smaller count will work for you. The last actions in scope 1 are to list all teams and to then use a filter array to filter out a team that matches the account related to the Project.

Flow-14-scope1.png

 

The actions in scope 2 are fairly straightforward and partly match those on the true side. Filtering the List Teams with a filter array throws an apply to each. We will then get our hands on a team id that is composed. A channel is then created and its id composed. An HTTP POST is used to add D365/PSA to the created team. There is no need for error handling for the delay action as on the false side a team is always created. The compose for JSON and the HTTP POST for tab match the ones on the true side.

Flow-15-scope2.png

 

The whole Flow can be seen in the image below. As always, the Flow can be downloaded from the TDG Power Platform Bank here.

Flow-16-Flow-expanded-edited.png

 

Disclaimer:
All my blog posts reflect my personal opinions and findings unless otherwise stated.
About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/