Moving Dynamics 365 Attachments to Azure Blob using Logic Apps

As I have blogged about previously, email and note attachments in Dynamics 365 can eat up a lot of valuable space in a Dynamics 365 database.

Attachment Management on Appsource

There are a number of 3rd party solutions on the market to address this, on AppSource is the Microsoft Labs “Attachment Management” solution.

1.attachmentmanagement
Attachment Management on AppSource

The solution is provided as a free App.  The caveat is that the App has no quick support mechanism (but it is supported) and has had a few issues during its evolution.  From the reviews it appears that for some it works, and for others it doesn’t.  I found that the instructions can be a bit unclear, but with some effort I was able to get it working.  I would advise against using this type of solution on large enterprise projects where a supported 3rd party solution may be a better choice.  E.g. North52

In order to get support, you can send an email to appsnotify@microsoft.com (and not via regular Dynamics 365 support channels). I have found that the Microsoft Labs team does make an effort to assist with any issues and usually will respond within at least a day.

Attachments not Moving Issue

notmoving
Attachments not going anywhere

An issue that I experienced recently was the “Move to Blob” process did not seem to move email attachments.  This could be due to the process timing out (exceeding the 2 minute plug-in threshold) or a particular attachment hanging up the process.  One of the solutions suggested by Microsoft Labs support was to use a Logic App instead of the plug-in.

3.d365attachmentcount
Before view – 6613 Email Attachments in Dynamics 365 – Move to Blob not working!

Logic Apps to the Rescue!

This particular Logic App can also come in handy when needing to migrate a bunch of attachments (possibly after a data migration).  You can monitor the progress in Azure and also easily identify potential issues.

This also is a great example of using Azure Logic Apps to address business issues in Dynamics 365.

Note that I personally DID NOT write this Logic App.  This was created by I believe Suresh Guduru and the Microsoft Labs team.  Just want to give credit where credit is due!

While the Logic App is an elegant solution, I found the instructions a bit confusing so here are the step by step method that I used to get it working.

You will need the following;

  • Dynamics 365 Instance
  • Azure Subscription
  • Ideally the Attachment Management tool installed from AppSource

Getting and Installing Logic App from GitHub on to Azure

The Logic App can be access via this link on GitHub.

1.github

Scrolling down, you will see buttons that will assist in loading the Logic App on your Azure subscription.  There are 3 available Logic Apps, 1 for note attachments, and 2 for email attachments, one with filters based on the “CreatedOn” date.

2.github2
Logic Apps

Clicking on the Deploy to Azure will then attempt to install the Logic App on your Azure instance.  If you manage multiple Azure subscriptions then I suggest you login first to the Azure portal you want to install the logic app on.

You will need to fill in some information;

4.installtemplate

  • Azure Subscription – self explanatory
  • Resource Group – I used the same group as my attachment containers, but you can create a new resource group
  • Location – ideally the same location as your Dynamics 365 instance
  • Login App Name – any name you want to give it
  • Logic App Location – again, ideally the same location as your Dynamics 365 instance
  • Dynamics Connection Name/Display Name – these can be anything you want but ideally descriptive of your Dynamics 365 instance.
  • Azure Connection Name/Display Name – again, anything you want but ideally descriptive.
  • Azure Blob Account Name – this needs to match the name of your Azure Storage Account
  • Azure Blob Access Key – Locate the Access key as per screenshot below.  Do NOT use the “Secure Access Signature”
4.accesskeys
Access key

Once you have filled in the values and accepted the terms, click Purchase.  Note that there is no direct cost for the Logic App but it will cost Azure credits (but very little).

6.installing

After a few moments the Logic App will be installed.  Note that it will attempt to run but will fail as we have not yet setup the various parameters and authorizations.

7.firsttimefailed
Logic App will fail on first run

Authorizing Dynamics 365

You will need to authorize the Logic App to access Dynamics 365.

Click on the API Connections and click to open the D365 API connection (here is where you can edit other settings if you mis-installed the Logic App)

apiconnections
API Connections

Click on the warning message “This connection is not authenticated”

8.Authorize1

You will need to click on the Authorize button.

8.Authorize2

Enter in your Dynamics 365 credentials, ideally use an Application Account.

8.Authorize3

You should get a message that Dynamics 365 authorization was successful.  Save the API connection.

8.Authorize4
API Authorized

Edit the Logic App Code

The next step is to update some of the variables in the Logic App code itself.  Click on “Logic App Code View” to view the code.

9.codeview

You will need to get the following

  • Dynamics 365 Unique Orgname

Note that this is NOT the name you find in the URL but the actual “orgXXXX’ name.  This cna be found in the Dynamics 365 Settings -> Customizations section.

10.orgname

In the Logic App Code, replace the CRMORGUNIQUENAME_TOBE_REPLACED with the org Unique name AND the crm region (e.g. North America -> crm, Canada -> crm3, etc)  Note that there are 2 places where this needs to be changed.

11.orgname.2
Update orgname

If your Azure notes container is NOT named the following:

  1. notesattachments
  2. emailattachments

Then you also need to update the container name in the Logic App Code:

12.containername
Container Name

The other thing to note is that there is a $top parameter that will indicate how many times for the Logic App to loop.  This is key for testing or running the Logic App in small batches.  However, it does run quick, and I found it processed about 100 attachments in about 18 seconds.

13.attachmentcount
How many attachments to process

Be default, the Logic App will run once every 300 days.  This is fine for testing but you may want to run it weekly or daily depending on your requirements.

"Recurrence": {

"recurrence": {

"frequency": "Day",

"interval": 300

},

"type": "Recurrence"

Finally, choose “RUN” to test the Logic App.  If the settings were correct, you should see it process.  If you get any errors, you can drill down for more information and update accordingly.  I found on my first few attempts it was the wrong App key used, the orgname and other minor misspellings.

14.runlogicapp
Success!

To see if the Logic App actually worked, you can view the Azure storage reports to see the attachment numbers in Dyn365 and Azure.

3.d365attachmentcount
BEFORE
13.d365attachmentcountafter
AFTER

Summary

 

The Attachment Management App from Microsoft Labs has evolved since being released last year.  Despite some criticisms, the app is supported and the team has listened and made updates (e.g. resolved conflicts with the D365 portal).

The Logic App is a nice addition to the overall solution and also highlights the power  Azure can provide to Dynamics 365.  Great work by the Microsoft Labs team!

award-good-job

Nick Doelman is a Microsoft MVP for Business Applications.  Join Nick and other MVPs at Dynamics 365 Saturday in Mississauga on May 5th!

4 thoughts on “Moving Dynamics 365 Attachments to Azure Blob using Logic Apps

  1. Great Article! Do you know if the Logic App limits itself to attachments on the entities you’ve configured inside the Attachment Management solution, or if it just uploads everything?

    Like

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