Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

By | May 10, 2018

Microsoft Flow allows us to connect and automate two disparate systems. And, the best part of Microsoft Flow is that both the systems need not be from Microsoft’s umbrella.

The way Microsoft Flow allows the communication between two different systems is with the help of plethora of connectors that’s available at our disposal. We can leverage these connectors based on our needs.

In our previous blog “Geo-code Dynamics 365 Records Using Microsoft Flow”, we see Microsoft Flow connect with Bing Maps Services and utilize the address information to find geo-coordinates. Now in this blog, we will be talking about such two connectors, Dynamics 365 and SharePoint and how can we leverage these to get our tedious job done with few steps of configuration.

Have you ever been in a situation where the Notes attachment are consuming the majority of your Dynamics 365 CE’s storage capacity? If yes, then this blog is specifically designed for you folks.

Say, whenever a user creates a Note with an attachment, you need a mechanism in place that will extract the attachment out of Note and upload it on SharePoint and remove the attachment from the Note leaving a URL back to the SharePoint location of the file in the Note body.

To achieve the above said requirement in the pre-Microsoft Flow era would have required a skilled developer having good knowledge of SharePoint and Dynamics 365 CE API, but, now it is possible to achieve this without writing a single line of code. See, this is where Microsoft is leaving no stone unturned to ease user’s lives.

Without further much ado, let’s dive directly to the steps that should be undertaken in order to achieve the said requirement.

Note: We have taken Account entity’s Notes for this example, you can work on any entity.

Step 1:

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

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Select your Organization Name, and also the entity, in our case it must be Notes.

Step 2:

Check whether the Note record that is created has a document attached to it, for this you need to add a Condition step.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Step 3:

On its success, validate if the Note record that is created is for an Account or not.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Step 4:

On the success of this condition, you must retrieve the Account record that is selected. The reason behind retrieving Account record is to get the Account Name, because the SharePoint folders are created in the format of AccountName_GUID. In order to achieve this, add an Action called “Get record”.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Step 5:

Now, let’s retrieve the Document Locations for the current record to see if there is an already defined path or not. For this choice, “Dynamics 365 – List Records” Action. Below are the details you should be passing. (You can modify this as per your need.)

Microsoft Flow - D365 Customer Engagement to SharePoint

Step 6:

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

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

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

Step 7:

For the true part of the condition, we would have to follow the below steps.

Step 7.1:

Here comes the main step of transferring file onto SharePoint. This time while choosing an Action, select the SharePoint connector, on selecting the SharePoint connector, it would give you the list of Actions that are SharePoint specific. From the list of Actions, select, “Create file”.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

 

So, let’s see each and every parameters that has been passed.

  • Site Address – The SharePoint Site address.
  • Folder Path – The path where the file needs to be stored, for e.g., /account/ (The Relative URL retrieved from Step 5.)
  • File Name – Name of the file.
  • File Content – The document body from the Note record.

The important piece here is, you need to convert base64 to binary. This is because, the Note’s attachment is stored in Base64 format and SharePoint accepts the file body in binary format.

Formula used: base64ToBinary(triggerBody()?['documentbody'])

Step 7.2:

The last and final step is to remove the attachment from Note and leave a URL back to the SharePoint path where the file has been stored, in Note’s body.

For this, choose action ‘Update a record’.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Step 8:

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

Step 8.1:

Create a file in SharePoint.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

The formula used for the last part of Folder Path is as below,

toUpper(replace(body(‘Get_record’)?['accountid'], ‘-‘,”))

Refer Step 7.1 for more details.

Step 8.2:

Update the Note record with the details. (Refer Step 7.2)

Step 8.3:

Retrieve the Parent Document Location. (This is needed because we would have to create a Document Location for the current record in Dynamics 365 CE.)

Choose, “Dynamics 365 Get List” action.

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Step 8.4:

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

Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

Parent Site or Location: This is the GUID of the parent Document Location retrieved in Step 8.3.

Relative URL: replace(replace(body(‘Create_file_3′)?['Path'],’/account/’,”),concat(‘/’,body(‘Create_file_3′)?['Name']),”)

Note:

  • If for the same record, you are creating two Notes with the same file name, then the latter file will override the former file.
  • Currently, Microsoft Flow needs to be created for specific entities, due to the fact that we don’t get name of the record and we need to retrieve it in order to form our folder name.
  • If folder doesn’t exist it creates by itself.
  • Even a user having User (no administrator access) role can create Microsoft Flow, but that user won’t be able to have the SharePoint or Dynamics 365 Customer Engagement connectors under his account. They need to create connectors under different account.
  • If the attachment is added afterwards, then to handle this, we need to create a different Microsoft Flow that would trigger on update of Notes.
  • Similar approach can be used in the case of stripping of attachments from an Email received or a Sent email.

Copy System and Custom Entities in Dynamics CRM

Leave a Reply