The built-in Teams actions of Power Automate do not enable you to post a message to Teams with an attachment from OneDrive or SharePoint. If you receive emails with attachments or generate regular documents, you might want to post them direct to Teams. With a custom connector, I will show you how this is possible. I have covered how to build the custom connector in my recent post How To? Build It! Custom Connector. In this post, I will use this connector and share with you my flow.
The flow is triggered by when an email arrives and is subject to the email containing a specific subject and also an attachment. This is to prevent unnecessary triggers. In order to get the team and channel GUIDs/IDs we must first use the Get a Team action, followed by the List Channels. You will note that I have chosen to save the file to my DamoBird365 site. On the result of the List Channels Action, I then perform a filter on the Array for a Channel Name, EmailAttachments. This could of course be the general channel or any channel of your choosing.
Next, we must save the attachment to SharePoint or OneDrive. In this instance, I have chosen SharePoint. I have also assumed that the email will only contain one attachment. This is to prevent an unnecessary Apply to Each loop. I have therefore used the first() expression in order to retrieve the first object from the name attachments array. These expressions are as per below.
File Name first(triggerOutputs()?['body/attachments'])?['name'] File Content first(triggerOutputs()?['body/attachments'])?['contentBytes']
Then using the custom connector, the complete build of which is demonstrated here, the attachment is posted to the Channel. To this custom connector, we must pass the Team ID retrieved from the Get a Team action. Then the first and therefore assumed only channel ID from the filter array. And finally, the attachment ID, retrieved from the attachments array. This the Etag value “{901B48BA-3D2E-41C5-88C2-BF588C9A3322},1” with the curly brackets, comma, and value removed. Hence the expression. Again I have used the first() expression in order to achieve this. Expressions for these two parameters are included below.
Channel-ID first(body('Filter_array'))?['ID'] Attachment ID first(split(replace(outputs('Create_file')?['body/Etag'],'"',''),','))
Also included in the action is your chosen message (content) and the encoding of the message (content-type). Most of these details can be explained by the Microsoft Documentation here. Because we must include the attachment id as HTML, we must use the HTML contentType.
The attachments contentUrl needs to be constructed using the SharePoint site URL and the Path dynamic value from the Create File action. The attachment’s name is retrieved from the SharePoint action too.
Whilst I did not test this myself, it would technically be possible to extend the custom connector to include mentions if need be.
Please let me know if you have made use of this idea or what your custom connector requirements are?
This is a great tutorial and exactly what I’m wanting to do. Thanks
Thought it was all going to well!
Getting the following error any idea where to dig, the flow or the custom connector?
Resource not found for the segment ‘channels’.
Have you tried the process in graph explorer first?? Presumably this is a connector error? Really hard to know what this is to be honest as I don’t have massive experience with connectors. Are you able to look at the history of the flow? Check that the guid is displaying ok? Try the same on the explorer?
Works in Graph Explorer so it must be a connector issue I’ll troubleshoot there (first custom connector for me so really shooting in the dark!)
What API permissions did you give your app?
How to create a custom connector in power automate with Graph API #PowerAutomate #GraphAPI https://youtu.be/EfsYrRQTfgw&t=465s
Finally got it to work when testing the custom connector but it won’t actually work when used in a flow and gives the most helpful error message “Internal Server Error Failed to Process Request”. So frustrating, almost wish I’d never seen this post then I could just say it’s not possible but now I know it is I have to get it working 🙂
Attention to detail! Wasn’t supplying the content type in the flow! All working now, thanks again for a great tutorial.
Brilliant Dan, appreciate the update! Glad you’re up and working.
Thank you so much!
Hi,
I am looking to share a Excel file in Teams group using Power Automate. In the Power Automate I have created a flow which creates an excel file (with all the names of employees in a Teams group). It is being stored on OneDrive but now I want the file link or the file to be shared on the Teams group (at the start of the week).
For more information you can check out my post (on Microsoft Power Apps Forum)
https://powerusers.microsoft.com/t5/Building-Power-Apps/Sending-a-file-at-the-start-of-every-week-in-MS-Teams/m-p/2248913#M563030
very useful video but what if needed to send more that 1 attachments?