Friday, May 22, 2020

Microsoft Flows - Send Email with List Item Attachments


In Flows, we don't have direct option to provide attachments references in Email actions. We need to get the content bytes and provide it either in JSON array format or individual file name and content values.


So, in order to send attachments in email,  we first need to get the content to a variable and then use it in the Send an Email action.

Flows has separate Sharepoint inbuilt actions to fetch list item attachments and also documents from a library. 

Below are the steps to send list items attachments in email

Initialize a Variable of type array to hold all the attachments.


Use Get Attachments action to fetch list of attachments in a given list item. This provides attachment name, unique file identifier. 


Use Apply to Each action to go through each attachment fetched from above action.
 
Fetch Attachment content using Get Attachment Content action by providing the unique file identifier

Then, using Append to array Variable action, add the attachment content in expected format. Here, while providing content bytes, we need to make sure to add ['$content'] to the reference as the byte content is in this parameter. body('Get_attachment_content')?['$content']

Note: If the attachment file is .txt, the "Get Attachment Content" action would return content into body, there is no JSON response here. In this case, we can simply use body('Get_attachment_content')

For "ContentBytes" property of attachments array, we can have a generic function as below
if( endsWith(items('Apply_to_each')?['DisplayName'],'.txt'), body('Get_attachment_content'), body('Get_attachment_content')['$content'])



Now, refer the attachments array variable in the Attachments section of Send an Email action.



In the same way, if we want to send any documents from library, we can use available action 

Also, if we want to send the attachments through Approval action, instead of "ContentBytes", we need to use "Content" in the Attachments JSON array.

Happy coding :-)




No comments:

Post a Comment