{D365 + Sharepoint + Microsoft flows} Delete attachments from corresponding SharePoint folder when a record is deleted in CRM using Microsoft Flows

Off late, I have been working with Microsoft flow and boy! I am liking it. It is cool and specially considering the fact that it simplifies whole lot of stuffs that you would normally require to write lot of code, make it even more appealing.

So here I was with a requirement.

There was OOB CRM-SharePoint integration enabled and the client wanted that whenever an entity record is deleted from CRM, the  documents in the corresponding SharePoint folder should be deleted as well.

Now the OOB behavior does not support that. Even if you delete the record, the folder corresponding to that is present in the SharePoint path with all the documents.

So what are my options here. Well some custom code that would run when the record is deleted in CRM and that code would be responsible for deleting the documents from the SP Folder. On top of that, if you are in online, you cannot use the client object model of SharePoint from your CRM plugins. You would require to make use of REST API of SharePoint which is way more complex that the client object model.

But wait, don’t worry. We have Microsoft flows. So let’s explore how can we achieve this using a few tricks and the very wonderful Microsoft flows. And let’s explore how this generic framework will work for all.

For my example, I will select the Contact entity.

Here is my contact record for which currently 3 documents are uploaded.


Below is the Sharepoint folder location for the same.


As you already know, when you enable OOB CRM-SP integration, the folder for each record for the first time gets created in the format of <Primary Attribute Name>_<Record Guid with ‘-‘>

In my case the folder name is – Darren Parker_69a0e5b988dfe311b8e56c3be5a8b200

I create a custom entity in CRM called – ‘SP Folder Delete Trigger’. And create a custom field called ‘Document Location’.

Whenever I would delete a contact record, I would create a new record of ‘SP Folder Delete Trigger’ and the Document Location field would contain the corresponding path of the Sharepoint folder. So if I delete the contact “Darren Parker”, I would create a record ‘SP folder Delete Trigger’ with Document Location as /contact/Darren Parker_69a0e5b988dfe311b8e56c3be5a8b200

How to create this record? Well I leave this up to you. Plugins, workflows, action? In what so ever you want.

Ok. But don’t create the record now. Let us first design a Microsoft flow which would do wonders. But how do I use it? Well, just like any other Microsoft feature, it is as easy as it gets. I will start from the beginning of how to enable flows for your organization. Readers who are already aware of the same can just skip the first few set-up steps.


  • If you are using first time, it will set this up for you. In either case you would land up in a screen like the one below.


  • Click on ‘Create from Blank’ button
  • In the search area, type ‘Dynamics 365’. It would show all the operations available for Dynamics 365.


  • Select the step – “Dynamics 365 – When a record is created”.
  • Enter your organization name and entity name. It will be present in the list of available options.


  • Click of ‘New Step’ –> Add an action


  • Select sharepoint and then select the Sharepoint – List folder action


  • Select your Site Url. It would be auto populated.


  • Now comes the important part, the folder identifier. Basically, it is the path to your folder. Since we are making a generic framework here, we won’t hardcode the folder path. Rather we would be passing the dynamic property. So what is the dynamic property

You remember we create a field called Document location which contains the path to the folder. So we set this field to the Document Location property of the created record.



  • Since we are using a dynamic property here, our flow works for any entity and any record. It’s not specific to entity type or record guid.
  • Now we have our folder. But we need to iterate through all the files in the folder and delete them right? So we need a for-each statement here. Let’s see how we can accomplish this.
  • Select Add step –> More –> Add an apply to each.


  • The for-each construct is being applied. In the output select ‘body’ from the previous List folder step


Remember, this body is nothing but the each of the files contained in the folder.

So the next step is to delete the files.

  • Select Add Step-> Add an action-> Sharepoint –> Delete File


  • As usual select the sharepoint url from the available options. For the file identifier, select the Id from the output of the previous step.


All set and done. Save the flow. Your flow is up and running.

Remember, we have not created the ‘SP folder delete trigger’ record. Since our flow is active now, we will do the same now. Here is the screenshot below of the same.


wondering the document location we mentioned for our example was different – /contact/Darren Parker_69a0e5b988dfe311b8e56c3be5a8b200. But the screenshot above shows a different. Well, it’s the same value. The value is just URI encoded. But why is it necessary?

This is because, the flow will internally use the REST API to query SharePoint and when our dynamic property ‘Document Location’ would be evaluated in the workflow, ‘/’ will not be a valid character and hence the flow would fail. Remember, this is very important to remember that you need to put the URL encoded values here to get the flow running. A simple mistake like this would take days to resolve and you would wonder what wrong you are doing.

So I created the record. My flow would fire now.

Go to My Flows and click on the information icon next to your flow.


Check for ‘Successes and Failures’


In my case, the flow has run and has executed successfully.

And if I go to my contact folder now, it is empty.



Awesome isn’t it? No code and you have set up something which talks between CRM and sharepoint.

And this flow works for all entity and all records. Just you need to set the Document Location property accordingly which can be done easily in CRM.


Hope you liked this. I will come up with some more interesting examples for flows. Till then, happy CRM’ing


-Debajit Dutta

(Visit our products page at www.xrmforyou.com to know about our offerings)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s