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’]),”)


  • 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

17 thoughts on “Move Dynamics 365 Attachments to SharePoint using Microsoft Flow

  1. phamilton


    I followed the steps and the flow worked fine. I have encountered an issue though. The new folder is not appearing in Document Associated Grid for the CRM record. When I open in SharePoint I can see two version of the folder. The only difference that I see is the flow generated folder has the GUID in lowercase, the other (default) folder location is in uppercase. Any idea how to rectify this because otherwise the solution isn’t very useful.


  2. drewc

    Good morning, I am getting an error on step 6: Invalid formula. The formula posted is : length(body(‘Retrieve_Document_Locations’)?[‘value’]). Is that the correct syntax?

    1. Inogic Post author

      The reason for it to fail could be, your action name would be different.

      By Action Name I mean is, for me it is. (Step 5)


      Which equals to Retrieve_Document_Locations. You need to look for the your action name and replace it in the formula.


  3. chrismahanes

    This worked like a charm! The content y’all come out with is amazing! Keep up the great work!

  4. cbh

    First of all, thanks for a very good guide. I am trying to implement the flow for the entity case / incident. I succeed with the first steps until the 8 to 8.4 – can you help me with a indication for what i need to replace in the different steps? The result is a succesful run but with the message “The input array was empty.”

    In Retrieve Document Location i have replaced account with case ( incidentid )

    It auto add a “Apply to each” step after the condition of length() – both within the yes and no. I have tried to do it without but can not do it. It puts Select an output from previous steps as body(‘Retrieve_the_Document_Location'[‘value’]

    here is the changes i have made:

    The toupper function looks as followed:

    In Retrieve the Parent Document Location i have typed this in Filter Query:
    relativeurl eq ‘incident’ and _regardingobjectid_value eq null and servicetype eq 0 and locationtype eq 0

    And in the replace() function the following:

    Best Regards,

    1. cbh

      I found that i did miss a small thing in the create file and now it is working fine – once again thank you for a good guide 🙂 I will try to do the same with mails primarily when when a mail in tracked through outlook with the function regarding to a case – do you recommend i follow this guide or the recurring-microsoft-flow-for-moving-email-attachment-from-dynamics-365-to-sharepoint?

      1. cbh

        I am now encounter another problem as it seems the flow is not triggered – only when i test it. Afterwards i try to create a note in CRM and nothing happens. Do you experience this problem?

        Best regards, Casper

        1. Inogic Post author

          I’d recommend you follow this blog.

          I have faced the issue of Microsoft Flow not triggering, but it was just an intermittent issue which got resolved on its own.

          I’d suggest you to follow the below approach, and I’m pretty sure, the flow would trigger after that.

          In your flow, you can do either of the below option and your flow should start triggering again.


          1. cbh

            Thank you for the answer 🙂 the trigger thing works again now.

            If i would like to set the flow to trigger when a e-mail is recieved or sent rather than every sunday – do you then know a good way to set it up? Would it be with a trigger of when a record is updated? and how would the condition look? When i tried to do it with the following condition:


            It fail, when i try to make a email as i need to save it before i can attach anything.

            It is just that you recomment the following in this guide:

            “Similar approach can be used in the case of stripping of attachments from an Email received or a Sent email.”

            Sorry for the simple questions, but i am pretty new in Microsoft flow and trying to figure it out.

          2. Inogic Post author

            To handle received emails, you need to have a Microsoft Flow on Create and to handle sent emails you need to have a Microsoft Flow either on Create or Update, depending upon your scenario.

  5. Geremy

    Great guide!

    Is it possible to use SharePoint folders based on the ‘Account’ hierarchy?

    Our situation:
    Client has SharePoint folder structure set up as the following:
    Accounts > Quotes
    E.g. filepath > Test Account > Quotes > Test Quote


    1. inogic Post author

      Thanks for your kind words.

      What you are asking for is bit tricky (and to an extent not possible), what you can do is, try checking for the SharePoint Document Location record for the quote that is currently being processed and based off of the result you can construct your logic.


  6. Krishna

    I have encountered the issue in condition 3.
    InvalidTemplate. Unable to process template language expressions for action ‘Validate_the_collection_length’ at line ‘1’ and column ‘2474’: ‘The template language function ‘greater’ expects two parameter of matching types. The function was invoked with values of type ‘String’ and ‘Integer’ that do not match.’.

    1. inogic Post author

      Could you please try verifying your left hand side of the operator?

      It should be length(body(‘Retrieve_Document_Locations’)?[‘value’]), where Retrieve_Document_Locations is the name of Step 5. (Note: The spaces are replaced with an underscore(_))


  7. Arun


    This is amazing solution. Is there any way to retrieve notes attached to the child records and store them into SharePoint trigger being update of a field in a parent record? Let’s say we update a Boolean field to yes in the parent with 5 child records and 1 document attached to each child record. We need to store all those notes into share point. I tried the above solution by changing the trigger point but did not work.


Leave a Reply

Your email address will not be published. Required fields are marked *