Send SharePoint Document as an Email Attachment using Flow

SharePoint Online allows users to share documents only as links. Documents links are created based on requested permissions, that can be shared with end users. Primary reason for this is to keep documents within SharePoint, giving more control over who has access and auditing around views/edits. Recently we came across a requirement, where users wanted to send document as email attachment, to users who don’t have O365 access or don’t want to go through the hassle of logging in.

To send a document as an email attachment, you can create a workflow using Microsoft Flow. Here are few more things to consider:

  1. User should be able to send the document from any site/library with this functionality enabled. Flow should not be bound to a specific library.
  2. We should be able to trigger the Flow from other areas of the site e.g. custom web part, not just document list view.

Follow the steps below to setup the Flow:

flow-overview
Flow Overview

Step 1 – When an HTTP request is received

Flow allows HTTP request as a trigger. You can send a POST request with specified parameters to trigger the FLOW.

Here are the parameters:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "emailSubject": {
            "type": "string"
        },
        "emailBody": {
            "type": "string"
        },
        "emailAddress": {
            "type": "string"
        },
        "fileRef": {
            "type": "string"
        },
        "fileLeafRef": {
            "type": "string"
        },
        "siteUrl": {
            "type": "string"
        }
    },
    "required": [
        "emailAddress",
        "emailSubject",
        "emailBody",
        "siteUrl",
        "fileRef",
        "fileLeafRef"
    ]
}

Step 2 – Get File Content

In this step, we retrieve the content of the document from specified site and file path.

We use Flow Action – ‘SharePoint – Get file content’

get-file-content-action.png

Specify ‘Site Address‘ and ‘File Identifier‘ from fields retrieved from previous step.

get-file-content

Step 3 – Send an email

File content retrieved in Step 2, are used here as an email attachment. We use ‘Office 365  Outlook – Send an email’ action to send the email, as in image below.

Note, ‘fileLeafRef’ is the file name with extension. File Content retrieved does not provide file name. FileleafRef can be passed in HTTP request as here, or retrieved using another action to get file properties.

send-an-email

Step 4 – Response

Returns an HTTP response as JSON, indicating completion of the flow.
In case of an error, flow will return respective error back.

response

Your flow is ready.

Testing

To test this, you need to send HTTP POST request to URL from HTTP Request action (Step 1).

postman-get-url

You can use Postman app to send the test request to this URL

postman-sample

Resources: 
- Get started with Microsoft Flow
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s