Get Attachments from a Teams message in Power Automate
In this #PowerShot, I will show you how to get attachments/ files from a teams message in Power Automate when a flow is triggered from the "For a selected message in Teams" trigger.
Let's Get Started!
Attachments/ files uploaded in teams chats are stored in SharePoint document libraries. In this post, we will set up a flow to trigger from a message selected in Teams and then get the file location of attachments added to the message and then get the file content of these attachments from the SharePoint document library and then finally send those in an email.
Step 1: Trigger: For a selected message in Teams-
Step 2: Action: Initialize variable- An array variable to collect all the attachment files.
Step 3: Action: Get Messages from Teams- To get more details from the messages. The trigger does not return the file location for the uploaded attachment and hence this action needs to be used to compare the message IDs and get the actual location of the files.
Step 4: Control: Apply to each- To iterate over each message and compare with the trigger message id.
//Loop 1 starts//
Step 5: Control: Condition- To compare the message ID of returned messages and the message on which the flow was triggered.
//Yes Branch (Here we are only going to use the Yes branch to get the attachment from a recent message. For older messages that might not be retrieved in the Get Messages action, an action to send an email notification mentioning that the file was not found can be added.)
Step 6: Control: Condition- To check if the message contains any attachments.
Step 7: Action: Send an email- To send an email with the teams message and notification that there were no attachments in the message.
Step 8: Control: Apply to each- To iterate over each attachment and collect the file content in an array.
//Loop 2 starts//
Step 9: Action: Get file content using path from SharePoint- To get the file content of the attachment from the SharePoint document library. (Ensure that you provide the correct path to the SharePoint site and document library. This may vary for different teams and individual users. The Get messages action returns with a location that has the exact URL and can be passed as dynamic content in this action)
Use the expression to extract the name field: items('Apply_to_each_2')?['name']
Step 10: Action: Append to array for variable- Construct the JSON for the files. As we are using the Send email from outlook action, the key value pairs will be as shown in the screenshot below.
//Loop 2 ends//
Step 11: Action: Send an email- To send an email with the teams message and all the attachments.
//Loop 1 Ends//
Setup in Action
In this post, we saw how to get the attachment content from a teams message using the SharePoint connector and send the attachments via email in Outlook. This flow also checks if there was any attachment in the message and only then attempts to get the file content. The same flow can be used for multiple attachments as well. The files can be used across different actions like AI Builder based processing, approvals etc. based on the use case.
More information on the Microsoft Teams connector here.
I hope you found this interesting and it helped you. Thank you for reading.