Moving Dynamics 365 attachments to Azure blob storage.

 

shutterstock_528925720

Everyone knows that storage space in Dynamics 365 is expensive, so normally if our customers need to store large amounts of unstructured data in Dynamics 365, we highly recommend to use SharePoint Online or Azure Blob Storage which is, even more, cheaper than SharePoint Online. In this article, I will tell you how to store all your unstructured data in the Azure blob with one simple Dynamics 365 plugin.

There are also several ready-made solutions for this topic, like Attachment manager from Microsoft Labs. Attachment manager can automatically push your attachments into the Azure Blob and creates stub files in your Dynamics 365 organization, once you want to access the file, Attachment manager will initiate background operation to download your file from Azure blob and replace it back in the CRM organization, so you would be able to access it, but this approach doesn’t work well on heavy files. My idea is to replace the file by the so-called  “.URL” link, so once the user would like to access the file he or she will be automatically redirected to the file content in Azure Blob. All modern browsers support this file extension.

Let’s start and create storage account where we will store our data from Dynamics 365. Go to the Azure portal and click on “Create a resource”, then go to section “Storage” and choose “Storage account – blob, file, table, queue”. You can see it below:

azurestorage

On the next screen you need to provide some more information regarding your blob storage, so let’s do this and click on the button “Create”.

createblob

Once you created your blob storage, the next step is to create a container. By creating containers you can easily split your unstructured data into different types, like entity or content. So let’s open our newly created blob and add a container to it.

addcontainer

Now you have your blob storage account with the container to store your data. Let’s create Share Access Signature to be able to access data in our storage. To do this let’s open our blob storage account and choose “Share access signature” from the settings section.

sas

Configure SAS settings and click on the button “Generate SAS and connection string”. In the bottom part of this screen, you will find your SAS token, which we will use in our code samples. Please be sure that you have properly configured the timeframe during which your SAS token will be valid.  As we want our users to have a direct access to the blob we need to properly configure our container. To configure the container open it and switch to the “Access policy” under “Settings” section.

accesss

Choose “Public access level” to “Blob (anonymous read access for blobs only)”. Now we are ready to write some code, which will access the Azure Blob storage. Below you can find AzureBlobManager class which will help you to access AzureBlob from Dynamics 365.

You can see that I’ve used “.cloud” extension for the overridden file in CRM. URL file type is a good fit for our solution, however, it is not possible to create files of such format in Dynamics 365, as CRM tries to automatically download the content and save it inside CRM.  My Idea is to register the same system behavior for another type of file. So, I’ve created .cloud file extension using Windows registry and applied the same system behavior for it as for .url. You can then apply these changes to all computers in your network by using Group Policy as we did. That’s pretty simple, the only thing that you need to do is to find .url registration key in your Windows Registry and recreate it with all subnodes for the .cloud extension. Now let’s create a plugin which will replace our data in Dynamics 365. The plugin is very simple and I will only cover replacement here. You will definitely be able to implement delete operation by your self.

As you can see the plugin is very simple, and of course, the code is not ideal and I don’t recommend to use it on production, but I hope that you got the idea how to manage Azure Blob storage and how to access it from Dynamics 365. Another important topic here is security. This solution is not very secure as you can actually access blob data by direct link. For our customer, it was not that important, because all files were encrypted and special software was used to decrypt files on the fly.

Leave a Reply