In this example I will show how to read the server settings directly from NAV code. Without the need to know the location of the config file or to create an external .Net dll. Also PowerShell isn’t needed, and you don’t need any information about the running environment. Only the tenant ID is important in case you are running in a multitenant environment, but the tenant ID is already available in C/AL code with the TENANTID key word.
This is the name of the .Net assembly that we are going to use. This assembly is available in the folder Dynamics NAV Service and in the RoleTailored Client folder. Normally, you need to put external assemblies to the Add-ins folder to use it. But in this case you can skip this step because the assembly is already available in the main program folder. Only when you want to pick up the assembly using the lookup buttons you will to need to copy it to the Add-ins folder, but this is only during development of code.
This assembly contains a huge number of classes and types that are internally used by Dynamics NAV. One of these classes is the ServerUserSettings that will give us access to the actual settings of the server that you are connected to.
All you need to do is create a variable of type DotNet and Subtype
Microsoft.Dynamics.Nav.Types.ServerUserSettings.’Microsoft.Dynamics.Nav.Types, Version=22.214.171.124, Culture=neutral, PublicKeyToken=31bf3856ad364e35′
Just copy and paste this value into the Subtype, and you can skip the step to copy the assembly to the Add-ins folder. In this example I have created a variable with the name ServerUserSettings.
By the way, the same can be done in NAV 2013 R2: replace Version=126.96.36.199 with Version=188.8.131.52
Usage of the DotNet variable
When you have created the variable, all you need to do is to write the next line of code:
ServerUserSettings := ServerUserSettings.Instance;
This will load the actual configuration settings from the server. And now you can read server settings like DatabaseServer, DatabaseName, ClientServicesPort, etc.
For example, to get the current SQL server, including instance, you can write this code:
ServerUserSettings := ServerUserSettings.Instance;<br /> MESSAGE('%1',ServerUserSettings.DatabaseName);
Multitenant safe database information
To get the database information, like servername, instance and database name, in both single tenant and multitenant situations you need to test the Multitenant property. This can be done with the following example.
In this example the variable NavTenantSettings is a DotNet variable, Subtype
Microsoft.Dynamics.Nav.Types.NavTenantSettings.’Microsoft.Dynamics.Nav.Types, Version=184.108.40.206, Culture=neutral, PublicKeyToken=31bf3856ad364e35′
In this examples I’m using undocumented features of Dynamics NAV. Although it works perfectly in the current (and previous) version, it is not guaranteed that it will work the same way in the future. Nevertheless, I have tested it in several environments and it proves to work stable.