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 :-)




Thursday, May 21, 2020

Microsoft Flows - Send an Email - SPUtility

Before we go into how to implement it, we shall quickly look into the advantages and disadvantages of using this approach to send emails In Flows for Sharepoint.

Advantages:
  • Using this approach we can avoid the complex configuration to send  Email to Sharepoint Group 
  • we can set any Sharepoint user in From Address, i.e. the Email can be addressed from any existing Sharepoint User. This will be helpful to send emails on behalf of Managers etc.
  • Can send HTML formatted email content. This helps us to design custom templates
Disadvantages:
  • Cannot attach files in the email
  • Can't send emails to external emails or mail boxes, i.e. the emails that doesn't have user accounts
  • From Address cannot be configured to any Mailbox or random name
Implementation:

we use SPUtility SendEMail Rest API method to send an email using HTTP Request to Sharepoint Action

As the SPUtility send email is a sharepoint related method, it only accepts Sharepoint entities to send emails and also if we provide a Sharepoint Group name, it would resolve it internally and sends emails to group members.

Below is sample screenshot and the JSON input we used to send email.

This email goes to Sharepoint Group "Demo Sharepoint Group", CCing Sharepoint group "CC Sharepoint Group" along with user "John". And this email is sent on behalf of "Riyana"
Also, we provided an external email id in TO list, but it doesn't send. 


Body:
 {  
       'properties': {  
         '__metadata': {  
           'type': 'SP.Utilities.EmailProperties'  
         },  
         'From': 'riyana@tmsdemo.onmicrosoft.com',  
         'To': {  
           'results': ['Demo Sharepoint Group','nonuseremail@external.com']  
         },  
         'CC': {  
           'results': ['CC Sharepoint Group','john@tmsdemo.onmicrosoft.com']  
         },  
         'Body': '<p>Demo EMail, <br/><br/>Sample email through Flows.</p>',  
         'Subject': 'Sample email through SPUtility'  
       }  
     }  

And the email in Outlook is