Move Attachments from Tracked Email to SharePoint using Microsoft Flow


Microsoft flows can be used to automate many workflows across multiple applications and services. This is basically designed for non-developers. To create a flow, the user specifies what action should take place when a specific event occurs.

In this blog, we will have a look on how we can add the attachments from the emails that are tracked in Dynamics 365 CRM to SharePoint

Systems included in Document Integration

  1. D365 App for Outlook
  2. SharePoint
  3. Dynamics CRM Online V9

Note: We have taken Opportunity tracked Emails for this example, you can work on any entity.

Let’s start with the steps that should be undertaken to achieve the above requirement.

Step 1:

Create a blank Flow template. Choose the Dynamics 365 Connector and trigger as, “When a record is created”.

Select your Organization Name, and the entity, in our case it is Email Messages.

Step 2:

Add condition to check whether the email is related to Opportunity and whether it has Status Reason as Received.

By default, the condition you can specify is basic. Change in to Advance mode and add the below formula

Formula used:



equals(triggerBody()?[‘_regardingobjectid_type’], ‘opportunities’),

equals(triggerBody()?[‘_statuscode_label’], ‘Received’)


Step 3:

For the true part of the condition, retrieve the Opportunity record to which the Email is tracked. The reason behind retrieving Opportunity Name is to create SharePoint Folders. Folders will be created on the format Topic_OPPORTUNITYGUID.


Step 4:

Now let us retrieve all the Activity Mime Attachments associated with the Email Message that is created.

There are two Attachments entities available for the selection.

The first one is the Attachment entity

The second one is the Activity Mime Attachment entity.

Step 5:

Retrieve Attachment related to the Activity Mime Attachment for the Body and File Name of the attachments.

Now steps to check the presence of Document Location starts

Step 6:

Retrieve the Document Locations for the current opportunity record to see if there is an already defined path or not.

Select “List Records” action and enter below fields

Step 7:

After retrieving the Document Location records, verify the length of the records in the collection.

Formula used : length(body(‘Retrieve_Document_Locations’)?[‘value’])

Step 8:

For the true part of the condition, follow the below steps:

Step 8.1:

Select connector as SharePoint and select Create file Action.

Enter all the details listed below:

1) Site Address: The SharePoint Site Address

2) Folder Path: The path where the file need to be stored. i.e. /opportunity/Topic_ toUpper(replace(body(‘Get_record’)?[‘opportunityid’],’-‘,”))

For Example the folder Name will be :


3) File Name: Name of the file

4) File Content: The attachment body

Step 8.2:

Delete the Attachment record from the Dynamics CRM Email message.

Here you need to select the first option of Attachments from the list.

Step 9:

For the false part of the condition below are the steps you should follow:

Step 9.1:

Create a file in SharePoint. Refer Step 8.1

Step 9.2:

Retrieve the Parent Document Location. This is needed as we are creating the document location by ourselves.

Step 9.3:

Create the Document Location record for the record that was just processed.

Parent Site or Location: This is the GUID of the parent Doucument Location retrieved in Step 9.2

Relative URL formula: replace(replace(body(‘Create_file_3’)?[‘Path’],’/opportunity/’,”),concat(‘/’,body(‘Create_file_3’)?[‘Name’]),”)

Step 10:

Delete the Attachment record from the Dynamics CRM Email message.

Here you need to select the first option of Attachments from the list.

Now you can test by sending email and tracking to Opportunity in D365 App for Outlook in Dynamics CRM. Document will be uploaded to SharePoint with specified location.

Share Story :