How to send inline image in MS team message using power automate?
Use Case
There is a size limit of 28 KB when we post a message on MS teams using power automate. But recently, I came across a new action “Send a Microsoft Graph HTTP request” that can be used for overcoming this limit. Using this action, we can easily post a message with inline images over 28 KB.
Scenerio
We have an image store in SharePoint ‘Site Assets’ library which will be posted on MS teams channel in the form of card:
Solution
1. Start with getting the content of the file from SharePoint library using “Get file content” action. I have hardcoded the file path but you can make it dynamic based on the use case:
2. Next, add two more actions to get the ids of Teams & Channel respectively. Add “Get team” action and select the team’s name from the dropdown. Post that, add “List channels” action to return the array or list of channels for a team.
3. To filter a channel from the “List channels” action, we will use “Filter array” action to get a particular channel. We will filter the channel by name using filter query. Pass the channel list from the output of previous action by selecting from dynamic window:
Click ‘Edit in advanced mode’ button on the bottom of filter array to enter the below filter query in the textbox:
@equals(item()?['displayName'], 'General')
Please note that I am posting card in ‘General’ channel, but you could modify the channel name as per the need.
4. Finally, we will use “Send a Microsoft Graph HTTP request” action to post a message with inline or embed image. We will be setting the teams & channel id dynamically. In the body, we need to pass the base64 content string along with content type of the hosted content. The maximum possible size of hosted content is 4 MB:
URI:
https://graph.microsoft.com/v1.0/teams/@{outputs('Get_a_team')?['body/id']}/channels/@{first(body('Filter_array'))?['id']}/messages
Method:
POST
Body:
{
"body": {
"contentType": "html",
"content": "<div><div>\n<div><span><img height=\"297\" src=\"../hostedContents/1/$value\" width=\"297\" style=\"vertical-align:bottom; width:297px; height:297px\"></span>\n\n</div>Good Morning!</div>\n</div>"
},
"hostedContents":[
{
"@microsoft.graph.temporaryId": "1",
"contentBytes": "@{body('Get_file_content')?['$content']}",
"contentType": "@{body('Get_file_content')?['$content-type']}"
}
]
}
Content-Type:
application/json
Output
Conclusion
Using “Send a Microsoft Graph HTTP request” action, we can directly call the graph apis for teamwork & communication space without using premium connector (Entra connector). As explained in this article, we can use the same action for posting message to overcome the message size limit of 28 kb. This action can be used for performing action which otherwise would not be possible with standard actions of the Microsoft teams connector like sending multiple inline images etc.
Source Code & Solution Package
Code & Solution package is available at:
How to mention a team or channel in channel message?
[…] Send inline image in MS team message using power automate (manish-solanki.com) […]
Bharadwaj
Can we use this method to post message in group chat too ?
If so can you please help with URI ?
Thanks.
Bharadwaj
Can we also do the same for a group chat ?
If so can you please share the URI for group chat ?
Nice work. Appreciated mate.
Manish Solanki
Thank you for your comment.
I tried posting group chat but could not succeed. It looks like this action does not work for all operations using graph api.
Here is the URL which I tried for group chat:/messages
https://graph.microsoft.com/v1.0/chats/
It throws the following error:
URI path is not a valid Graph endpoint, path is neither absolute nor relative or resource/object is not supported for this connector. Resources: teams,me,users Objects: channels,chats,installedApps,messages,pinnedMessages. Uri: https://graph.microsoft.com/v1.0/chats/xx-xx-xx/messages
Nattakit
When try send to group chat, We need to change action ‘List channels’ to ‘List Chats’ ? Can shown the flow?
Kiran
Hi, Thanks for providing this solution as it helped me a lot as I am very new to this world and don’t have any coding background. Can I send a gif also in the same way or would it need any change in the code?
Manish Solanki
I am glad that you found it useful ๐
Yes, the code should work for gif image as well. Moreover, you may download the unmanaged solution from the MS PnP sample gallery. You can find the link for download in this article. Just import the solution in dev power platform environment and play with it.
Please let me know if you face any issue.
Thank you. Cheers!