Microsoft Teams messages from Power Apps

Header

Microsoft Teams messages from Power Apps

Posting Microsoft Teams messages from Power Apps is possible without use of Power Automate and in the post will show you how to do it.

Updated at January 2021:
Added information how to get Id of posted message, added navigation and applied some corrections.

Navigation


Introduction

I decided to cover this topic due:

1.) Facing such challenge during build of one solution

2.) Microsoft Teams are one of the key tools use by people for collaboration and finding the ways to create communication between Power Apps and Teams Channel might be very important for many solutions. As Maker of Power Apps it is my duty to show alternative to Power Automate:P


Preparation

In the first part of this post I will cover preparations of the following elements:

  • Connecting to Microsoft Teams
  • Getting Team Id
  • Getting Channel Id

These elements are required to use correct post action.

Image with Post Message

Connect to Microsoft Teams

First you need to add connector of Microsoft Teams to your App:

  1. Select Data Sources from View tab
  2. Click New Connection
  3. Type TEAMS
  4. Click Microsoft Teams and then Connect
  5. You should see now MicrosoftTeams in the list

Now when you will start to type in Microsoft Teams you should see a list of all methods or speaking in Power Automate terminology actions:

Microsoft Teams methods

Reference to all Methods/Actions click here


Get Team Id

Next step is to get the Team Id we can do it by Action GetAllTeams, I connected it with OnSelect action of button and Set to create variable:

Set(varTeams,MicrosoftTeams.GetAllTeams())

NOTE:
It is important to understand that in returned data VALUE is table.

Result was following variable:

Based on the above easy access to information from GetAllTeams is to use gallery by as items adding:

varTeams.value

The Alternative option is to use GetAllTeams().value as Items in the gallery. Without calling it via variable.

Result of variable in gallery

As the property of the labels in gallery just use ID or / and dispayName.

Now to retrieve single Team Id just use on the label or variable reference to:

 Gallery3.Selected.id

NOTE: Your galleries should always follow the naming convention of your Application.


Get Channel Id

Channel Id is connected with Team which is selected, so I first need to know Team Id to “drill” to the channel level.

If you have the Team id (for instance showed as label or variable) you need to use GetChannelsForGroup to retrieve all channels from selected Team.

Example of GetChannelsForGroup:

Set(varChannels,MicrosoftTeams.GetChannelsForGroup(lblTeamsID.Text))

As return information is having the same schema (@odata.context, value properties at the first level) as in the case of getting Team Id. I’m repeating the same configuration so creating gallery and for Items property adding varChannels.value.

Now I’m ready to start posting to my selected channel.


Post message

With everything read I thought that now it will be easy to just us Action PostMessageToChannelV3 to post Microsoft Teams messages from Power Apps, but it was constantly returning me error…

The challenge was with required body property to work:

Another image with Post Message method

After several hours I finally found the JSON schema / definition of the actions and the following information resolved my problem:

"description":"body",
"required"[
"content",
"contentType"
],

The proper function will looks as follow:

Whole post message explained

ContentType can take two values:
– html
– text

Result:

Result of Post Message

To enable for Users option to work with html use the Rich text editor control


Bonus: Get Id of posted message

“How to capture id of a posted message?” It is one of the questions which I received to this blog, and it makes a lot of sense to answer it.

The fastest way is to use variable (Set or UpdateContext – depending on your needs) “around” post method. You can do it in two ways.

First way as record

Set(varResult,
MicrosoftTeams.PostMessageToChannelV3(
lblTeamsID.text,
Gallery1.Selected.id,
{content:"<p>Test</p>",contentType:"html"},
{subject:"Test"}
)
)

After executing PostMessage variable will capture the result of post action and you will see:

varResult showing id

To use the id now you just need to refer to variable and it’s property:

varResult.id
varResult in the Canvas

Now knowning the return result you can refer directly to id property.

Second way get id directly

Set(varResult,
MicrosoftTeams.PostMessageToChannelV3(
lblTeamsID.text,
Gallery1.Selected.id,
{content:"<p>Test</p>",contentType:"html"},
{subject:"Test"}
).id
)

Please notice that there is very small but important diffrence. After post method I added .id

MicrosoftTeams.PostMessageToChannelV3().id

With that I’m directly refering to id property of the return information. As the result my variable is populated with the id (no record this time):

varResult as id property

Bonus 2: Reply to posted message

When you will post new message you can save message ID to variable (and later on to your back-end) with that done you can use Action PostReplyToMessageV2:

Showing how to Reply
MicrosoftTeams.PostReplyToMessageV2(
"TeamID",
"ChannelID",
varSelectedProject.MessageID,
{
content: "your html",
contentType: "html"
}
)

Result:

Result of Reply

Summary

This is how you can start to post and answer to messages in MS Teams directly from Power Apps. Think it is good practice to not add additional moving elements to the configuration of solution if we don’t need them. Instead of having App and Flow you can have just App.

I’m hoping this approach will add new technic to your arsenals as the same way can be used for other connectors.

Thank you for reading and hope this will be useful for your during your development time.

As always please let me know your thoughts.

5 thoughts on “Microsoft Teams messages from Power Apps

    1. Hi Jem,
      Thanks for your question.
      You need “close” your post call in Set or UpdateContext to capture the id of the message.
      Example: Set(varResult,MicrosoftTeams.PostMessageToChannelV3(TEAMID,CHANNELID,{content:”Test”,contentType:”text”},{subject:”DEMO”}))
      After posting the message in the varResult you will see record with single property which is id. To use it or save it somewhere you just type varResult.id

      Hope it make sense. Shortly will update my blog with this information.
      Regards
      Dawid

Leave a Reply

Your email address will not be published. Required fields are marked *