Recently, I got the question on how to get a dependency tree from a bunch of Microsoft Dynamics 365 Business Central apps. In other words – how do I know in which order I have to import my apps to respect the dependencies.
Well, I didn’t have a ready-made script available. I only had a script my colleague provided me – so I took that as a starting point, and spent an evening in trying to put something together – as it IS a very interesting question ;-).
To be honest, we don’t need this in our company in our build pipeline, nor in our release pipeline. Nevertheless – I have heard the question multiple times, so Let’s .. Get .. Started .. ;-).
It’s obvious we will use PowerShell for this, as that’s what you will use to publish apps in an environment. You use it in build pipelines, in release pipelines, and even when you would just import in a one-off sandbox.
In PowerShell, we also have the CmdLets that Microsoft provides to manage our environment.
So .. Let’s start by linking the script, which you can find on my GitHub here: https://github.com/waldo1001/Cloud.Ready.Software.PowerShell/blob/master/PSScripts/NAV%20Extensions%20v2/GetDependencies.ps1
(I’m just providing a link, as when the script would change, you would alway see the up-to-date version of the script).
Some brief explanation
The script assumes you have a bunch of app-files somewhere. In this case, on the “c:\programData\NavContainerHelper” folder, because I’m using docker in order to run Business Central PowerShell CmdLets. You see I’m using the “Get-NAVAppInfo” cmdlet to get information about my apps. This information includes dependencies – which I will obviously need for my script. I then create my own collection of apps in PowerShell to be able to loop and do my thing.
The function AddToDependencyTree is a recursive function (wow – I didn’t use recursive functions for ages :-/) where all the “magic” happens. By looping the apps in my collection, I will always respect the dependencies by recursively drilling down into the dependency tree, and handle the lower-leveled apps by adding (or modifying) them with a lower “order” in the result-array (the DependencyArray variable).. . When completely looped, I have a new array including a decent “ProcessOrder” property in my collection.
On the bottom of the script, you see the actual loop, where I loop all the apps, and add them to the dependency tree.
To make it somewhat easier for me, and to simulate a somewhat complex dependency tree, I created this script:
It would replace the “Get-NAVAppInfo” looping through files, as it will build a “$AllApps” variable to be able to loop through. Just a tip ;-).
I did briefly look online if I could find any blogpost that already handled it – but only briefly. If I missed a solution (which is probably better than this one) – I apologize. There are so many contributors lately, it’s hard to keep track, catch up, … you know what I mean ;-). So, if you have a better (or worse ;-)) solution, don’t hesitate to mention that in the comment section.