Read Server Settings from C/AL code

7 Apr

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.

Microsoft.Dynamics.Nav.Types.dll

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=8.0.0.0, 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.

040715_2157_ReadServerS1.png

By the way, the same can be done in NAV 2013 R2: replace Version=8.0.0.0 with Version=7.1.0.0

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;
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=8.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′

 040715_2157_ReadServerS4.png

040715_2157_ReadServerS5.png

Disclaimer

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.

12 thoughts on “Read Server Settings from C/AL code

  1. Pingback: Read Server Settings from C/AL code - Microsoft Dynamics NAV Thoughts - Dynamics User Group

  2. This is very nice 🙂

    It would also be interesting to read the UserSettings from C/AL code. The assembly you’ve posted here contains a member ‘Microsoft.Dynamics.Nav.Types.UserSettings’, but I can’t find a way to instantiate it with the info of the current session.

    Do you have any idea how to accomplish that?

    • Hi Aize,

      Most of the info that is in the UserSettings class can also be read directly from C/AL, like UserName, UserGuid and TenantID. Furthermore, the UserSettings class contains data that can be found in the system table 2000000073 User Personalization.

      I haven’t figured out if there is a way to get the running values, but I doubt if it give any extra usefull information that cannot be found using C/AL code.

      /AJ

      • I’m using Peik’s solution to start NAV in configuration mode from the Profile List (https://dynamicsuser.net/nav/b/peik/posts/managing-dynamics-nav-profiles-from-the-profile-list-page) but it always starts the client based on the default ClientUserSettings.config. But my client is using Citrix and several different ClientUserSettings to hit the exact instance and authentication method and even DnsIdentity.

        I would therefore like to get the path and filename of the used ClientUserSettings in C/AL. Anyone with an idea to find that?

        • Made a solution extracting the -settings parameter from the command line, using System.Environment in mscorlib:
          System.Environment.’mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
          l_ClientEnviron.CommandLine

          (Just remember setting RunOnClient to get the RTC client. Otherwise you get the server.exe)

  3. Hi
    When i use this code ServerUserSettings := ServerUserSettings.Instance; i got one error message “dotnet varible has not been instantiated …..” . i m using Dynamics Nav 2013.

  4. Hi,

    I had to change this line…

    NavTenantSettings := ServerUserSettings.TenantConfiguration.Tenants.Get(TENANTID).TenantElement.Settings;

    to this…

    NavTenantSettings := ServerUserSettings.TenantConfiguration.Tenants.Get(TENANTID).Settings;

    I’m on NAV 2015, build 40459, but I don’t think that makes any difference.

    Does anyone know how to do this in NAV 2016?

Leave a Reply

Your email address will not be published.