Power Automate: Retrieve teamId and channelId from Teams Trigger

In an effort to reduce the number of edits a user would have to make when importing a Power Automate Flow which responds automatically to a new Teams message, I wanted to retrieve the Team ID and the Channel ID to use later in the Flow.

There were a number of available solutions (e.g. Solved: Re: Get Teams name and Teams Channel – Power Platform Community) however they all seemed to use a complex expression to get the required information.

Looking at the raw information provided by the trigger (add the trigger to a Flow, run it, then examine the run history to retrieve the raw data) showed that it should be possible to retrieve the information required without using a complex expression. Here’s the information in the trigger header and body:

     “headers”: {
         “Transfer-Encoding”: “chunked”,
         “Vary”: “Accept-Encoding”,
         “Strict-Transport-Security”: “max-age=31536000”,
         “request-id”: “25417c44-XXXX-XXXX-XXXX-1795a8963e18”,
         “client-request-id”: “25417c44-XXXX-XXXX-XXXX-1795a8963e18”,
         “x-ms-ags-diagnostic”: “{\”ServerInfo\”:{\”DataCenter\”:\”UK South\”,\”Slice\”:\”E\”,\”Ring\”:\”3\”,\”ScaleUnit\”:\”003\”,\”RoleInstance\”:\”LO2PEPF00000033\”}}”,
         “Preference-Applied”: “odata.track-changes”,
         “OData-Version”: “4.0”,
         “Timing-Allow-Origin”: “*”,
         “x-ms-apihub-cached-response”: “true”,
         “retry-after”: “180”,
         “Date”: “Tue, 03 Aug 2021 19:19:38 GMT”,
         “Location”: “https://uk-001.azure-apim.net/apim/teams/…/trigger/beta/teams/<TeamID>channels/<ChannelID>/messages?%24top=50&triggerState=$deltatoken~…”,
         “Content-Type”: “application/json”,
         “Content-Length”: “1439”
     “body”: {
         “@odata.type”: “#microsoft.graph.chatMessage”,
         “etag”: “…”,
         “messageType”: “message”,
         “createdDateTime”: “2021-08-03T19:15:19.411Z”,
         “lastModifiedDateTime”: “2021-08-03T19:15:19.411Z”,
         “importance”: “normal”,
         “locale”: “en-us”,
         “webUrl”: https://teams.microsoft.com/l/message/<ChannelID>1628018119411?groupId=<TeamID>&tenantId=<TennantID>&createdTime=1628018119411&parentMessageId=162XXXX11XXXX,
         “id”: “…”,
         “from”: {
             “user”: {
                 “id”: “…”,
                 “displayName”: “Andy Dawson”,
                 “userIdentityType”: “aadUser”
         “body”: {
             “contentType”: “text”,
             “content”: “This is another test, this time to see if the variable I need is directly readable…”
         “channelIdentity”: {
             “teamId”: “<TeamID>”,
             “channelId”: “<ChannelID>”
         “attachments”: [],
         “mentions”: [],
         “reactions”: []

From this, the expressions to retrieve the TeamID and ChannelID are respectively:

  • triggerBody()?[‘channelIdentity/teamId’]
  • triggerBody()?[‘channelIdentity/channelId’]

TeamID From Flow TriggerBody

Correcting Legacy Microsoft Teams Settings

I recently noticed that some of the Microsoft Teams that we had in use exhibited different behaviour to others that had been created more recently. ‘Recently’ (for varying definitions of ‘recent’) created Teams allowed users to edit and delete their messages, whereas a few older Teams did not. The older Teams were all created very early during our rollout of Teams, when Microsoft Teams itself was relatively new.

Checking the Teams messaging policies showed that the correct settings were in place to allow users to edit and delete their messages:

Messaging policy settings

It appeared that these settings were not however being applied for the legacy Teams. Checking in the individual Team settings showed that these were overriding the global settings applied:

Individual Team settings

Note also that in the above screen shot, the ‘Owners can delete all messages’ setting was also unchecked, although this turned out to be the only Team for which this was the case.

I could go through each Team individually and manually check the Team settings, however PowerShell allows us to perform the same actions quickly. The following PowerShell checks for the user Team settings being mis-configured and corrects the settings:


$teams = Get-Team | where {$_.AllowUserDeleteMessages -eq $False -OR $_.AllowUserEditMessages -eq $False}

foreach ($t in $teams) {
     Set-Team -DisplayName $t.DisplayName -GroupId $t.GroupId -AllowUserEditMessages $True -AllowUserDeleteMessages $True

For the other mis-configured setting mentioned, use ‘AllowOwnerDeleteMessages’ as the search criteria.