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

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

  1. cbh@bikubenfonden.dk'cbh

    Hi
    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:
    toUpper(replace(body(‘Get_record’)?['incidentid'],’-‘,”))

    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:
    replace(replace(body(‘Create_file_2′)?['Path'],’/incident/’,”),concat(‘/’,body(‘Create_file_2′)?['Name']),”)

    Best Regards,
    Casper

    Reply
    1. cbh@bikubenfonden.dk'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?

      Reply
      1. cbh@bikubenfonden.dk'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

        Reply
        1. Inogic Post author

          I’d recommend you follow this https://www.inogic.com/blog/2018/05/recurring-microsoft-flow-for-moving-email-attachment-from-dynamics-365-to-sharepoint/ 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.

          Thanks!

          Reply
          1. cbh@bikubenfonden.dk'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:

            @and(equals(triggerBody()['_regardingobjectid_value'],’incident’),greater(triggerBody()['attachmentcount'],0))

            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.

  2. cmahanes@provance.com'chrismahanes

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

    Reply
  3. drewc@electrotech-inc.com'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?

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

      Retrieve_Document_Locations

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

      Thanks!

      Reply
  4. phamilton@brightwork.com'phamilton

    Hi,

    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.

    Thanks!

    Reply

Leave a Reply

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