Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

By | May 22, 2018

Introduction:

In our last blog, we spoke about how to move attachments from Notes to SharePoint.

In this blog, our focus would be to discuss on moving Email Attachments and leaving a link back in Notes for the user to locate the attachments.

Let’s consider a scenario, a service based company who get a lot of emails from clients with Attachments in almost all of them. The Attachments are taking up a lot of space in their Dynamics 365 storage. They want a mechanism in place to move those attachments in a timely manner over to the SharePoint with a link back to the SharePoint path for the Service Reps to access in the need of the hour. And, not to forget majority of those emails are regarding the Case entity.

Without further much ado, let’s delve into the steps we need to configure on Microsoft Flow to make this happen.

At the heart of everything, like the requirement is, “The attachments should be moved over in a timely fashion.” We need to configure recurring Microsoft Flow.

Steps for setting up Recurrence

Step 1:

Select the “Schedule – Recurrence” Trigger.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Step 2:

Configure the recurrence Interval.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Now, let’s configure the extraction steps.

Steps for moving Email Attachments

Step 1:

After setting the recurrence, next step is to get the past time. This is for the reason that, we’ll be moving all the emails that have entered Dynamics 365 over the last week.

For this choose the Action as “Date Time – Get Past Time”. (Where Date Time is the connector and Get Past Time is the Action)

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Step 2:

After this convert the time that has been obtained in the above step to the local time.

For this choose the Action as “Date Time – Convert time zone”.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Step 3:

Retrieve all the Emails, since last Sunday.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

In the Filter Query, we have concatenated the Time part to the Date obtained in the previous step.

Formula Used: concat(body(‘Convert_time_zone’),’T00:00:00Z’)

Note: The time format that we are using here for the comparison is in UTC format.

Step 4:

Now, loop through all the Email Messages that we have retrieved.

For this choose the “Add an apply to each” step.

Step 5:

Now, add the Condition step.

In this step begin with checking the condition as shown below.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Formula Used: @and(equals(items(‘Loop_through_Email_Messages’)?[‘_regardingobjectid_type’], ‘incidents’), greater(items(‘Loop_through_Email_Messages’)[‘attachmentcount’], 0))

Note:

  • Since, the Basic Mode only allow one condition to verify, we need to use the advance mode to check multiple condition.
  • We would only be proceeding if the Email is regarding a Case entity and the Email has attachments.

Step 6:

In the true part of the condition, Retrieve the Case by using “Dynamics 365 – Get record” action. This is needed in order for us to create the folder in SharePoint.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Item identifier: The Regarding selected above is from the retrieved Email step.

Note:

  • In this blog, we are considering the basic scenario where all the Case records have the Document Location pre-created in Dynamics 365 CE. And, the format of the folder in SharePoint is “CaseTitle_CaseGUID”.
  • For the complex scenarios like the Document Location could be custom or the Document Location doesn’t exist in Dynamics 365 CE please referMove Dynamics 365 Attachments to SharePoint using Microsoft Flow

Step 7:

Next, retrieve Activity Mime Attachments associated with the Email.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Filter Query: Email Message parameter is the Email GUID. This is available throughout all the steps.

Note:

There are two Attachments entities available for the selection.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

The first one is the Attachment entity.

The second one is the Activity Mime Attachment entity.

  • For this step, we need to select the second in the list.

Step 8:

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

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Entity Name: Attachment entity is the first one from the list.

Filter Query: Dynamic Value “Attachment” is the Attachment Id that we get when we retrieve the Activity Mime Attachment. The usage of Dynamics Value “Attachment”, would enclose the entire retrieve “Attachment” step in an “Add an apply for each” loop.

Step 9:

Here comes the final step of creating a file in SharePoint.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Folder Path: /incident/Case Title (obtained from RetrieveCase step)_Case GUID(obtained from RetrieveCase step)

Formula used for Case GUID: toUpper(replace(body(‘RetrieveCase’)?[‘incidentid’], ‘-‘,”))

Note: Since, in this example we are only considering Emails related to case therefore the hard-coded incident in the path.

File Name: File Name (Obtained from RetrieveAttachment step)

File Content: Body (Obtained from RetrieveAttachment step)

Formula used for File Content: base64ToBinary(items(‘LoopThroughAttachments’)?[‘body’])

Step 10:

Create a Notes record against the Email for the reference of SharePoint path.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Dynamics Value Path is obtained from the Create File step.

Step 11:

Finally, delete the Activity Mime Attachment.

Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

Entity Name: The Attachment entity selected here is the second one from the list.

Item Identifier: Activity Mime Attachment Id obtained from the RetrieveActivityMimeAttachment step.

Conclusion:

Using the simple steps described above the user can step up recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint.

Export Dynamics CRM Reports

15 thoughts on “Recurring Microsoft Flow for moving Email Attachment from Dynamics 365 to SharePoint

  1. chrismahanes

    This is a great article! I am having a failure on step 7. When attempting to retrieve the MIME Attachments based on the email record I get a failure as this attribute does not exist. Me being a curious one I wanted to see what attributes the secondary Attachments had. Running a generic flow, since we cannot see these fields in advanced find. I found in the JSON that is return values for ItemInternalID, Attachmentid, mimetype, body, filename, filesize, versionnumber and subject.

    I then altered my flow to flip the two attachment list records. and now I am receiving an error on the create file step Unable to process template language expressions in action ‘Create_file’ inputs at line ‘1’ and column ‘2486’: ‘The template language function ‘base64ToBinary’ expects its parameter to be a string. The provided value is of type ‘Null’. Please see https://aka.ms/logicexpressions#base64ToBinary for usage details.’.

    Reply
    1. Inogic Post author

      For the attribute does not exist issue,
      There are two entities with the same name when we look for Attachment in Microsoft Flow. You need to select the second one in the list. Kindly, verify that you have selected the second one.

      For the base64ToBinary issue,
      The function expects a string parameter, please verify whether you passing a string parameter or a different type of a parameter. In our case below is the formula that we have to use.
      base64ToBinary(items(‘LoopThroughAttachments’)?[‘body’])

      For the expression is an invalid issue,
      Kindly verify your Step 6 name, and accordingly use it in the formula.

      For us it is RetrieveCase,

      So, based on the name we need to use it the formula, if it Retrieve Case, then in the formula it would be used as Retrieve_Case, so the final formula would look like below.
      toUpper(replace(body(‘Retrieve_Case’)?[‘incidentid’], ‘-‘,”))

      Hope this helps!

      Reply
  2. chrismahanes

    Thank you for the assistance. After some testing I have the below
    1) I have verified I have selected the second attachment entity and still receive the
    “status”: 400,
    “message”: “Could not find a property named ‘_objectid_value’ on type ‘Microsoft.Dynamics.CRM.attachment’.”,

    When I select the first attachment it does allow the flow to continue but then fails on the Create sharepoint File step for “InvalidTemplate. Unable to process template language expressions in action ‘Create_file’ inputs at line ‘1’ and column ‘2480’: ‘The template language function ‘base64ToBinary’ expects its parameter to be a string. The provided value is of type ‘Null’.”

    Will it always be the main Attachment the first and the Attachment Activity Mime Type being the second entity. Could there be a chance it is reversed?

    2) I have fixed the base64ToBinary issue.
    3) Still receive the error on the toUpper expression. I have verified on the yes side of the condition my get record step is call RetrieveCase. I am using toUpper(replace(body(‘RetrieveCase’)?[‘incidentid’],’-‘,”)) which I was able to workaround by just passing the Case variable as it is just the GUID

    I would be happy to send you a copy of an export if that would help? as I cannot post screenshots in here

    Reply
  3. Phong Nguyen

    Great article! Curious here. If we were to go down this route and then later had to move our SharePoint URL that houses all of these attachments, how would that work?

    Reply
    1. Inogic Post author

      Thanks a lot for your kind words.

      By moving SharePoint URL, if you mean changing the Site in SharePoint, and, if you want all the files from SharePoint site A to SharePoint site B, then you would have to write the logic of moving the files from SharePoint site A to SharePoint site B (this could be in Flow or a windows application of any type of one time arrangement to move these files). Thereafter, come back to your original flow and make the modification to point to the new site.

      Reply
  4. Phil

    Great article – it might be worth publishing a screenshot of the Flow as a whole – just so that newbies get a good feel for the layout of the Flow itself. I’ve put together a Flow for a Dynamics environment and it works well, but I’m unsure if it matches the layout of your Flow.

    I’m also not sure if the two ‘Attachments’ as listed might not have switched places! This seemed to cause me the most grief when creating my Flow for this.

    Reply
    1. Inogic Post author

      Thank you for your suggestion regarding the screenshot, will surely keep this in mind for our next blogpost.

      Also regarding the ‘Attachments’ switching the places, it didn’t happen at the time of writing the blog, but cannot say for sure, that it won’t happen in future. Need to be careful while choosing the ‘Attachment’.

      Reply
  5. Bernard

    Great stuff, thanks for sharing! How would you go about this for Emails where the Regarding field is empty? Would there be a away to store the attachments in the Document Locations of the associated Activity Parties?

    Reply
    1. Inogic Post author

      Hi,

      Thanks for your kind words.

      The requirement that you are looking for could get bit tricky plus complex. If we could get our hands on the activity parties that are associated to the email message, then we can proceed doing what you have asked for.

      As far as my knowledge goes, we just get a string of email ids separated by “,” for all the recipients, now, if this is the case, then extracting the email ids and then retrieving the corresponding activity party could get bit messy and tedious.

      Rather what I could suggest is, you can move the attachment of such emails to a separate folder which will be specifically kept aside for such orphan records. And, after the extraction logic of attachments from CRM is completed, we can notify the system user about this entire movement of the attachments from CRM to SharePoint in an orphan folder.

      Hope this helps!

      Thanks!

      Reply
  6. Lee

    Hi, good blog. I have one question:

    In the last step – step 11, regarding “Item Identifier: Activity Mime Attachment Id obtained from the RetrieveActivityMimeAttachment step.” , there is no option for me to choose Activity Mime Attachment Id in RetrieveActivityMimeAttachment step. There are only 5 options available – Attachment, Body, File Name, MIME Type, and Subject. Is something missing or the step name is wrong…?

    Reply
    1. Inogic Post author

      Hi,

      Like you know there are two Attachment entities that we see in the list, try selecting the other one and this should do the trick for you.

      Let us know if this helps you or not.

      Thanks!

      Reply
  7. Brad

    Hi

    Please can you provide details on Step 4 exactly in the condition what expression did you use in the advanced mode or can you be clear what you used in the basic mode. If you could provide a screenshot of the condition even better. Your assistance would be greatly appreciated

    Brad

    Reply
    1. inogic Post author

      Hi,

      For the step 4, below is what you have to do,

      Select the Control connector as highlighted in the below image,

      Microsoft Flow

      Select the Apply to each action as highlighted in the below image,

      Microsoft Flow

      For the Apply to each action, you need to select the collection in which you need to loop through, so, in our case it should be the collection retrieved in the Step 3.

      The list of collections, should be available in the Dynamic Content tab, as shown in the below screenshot.

      Microsoft Flow

      Hope this helps!

      Reply

Leave a Reply

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