How to Retrieve Server Settings of BC On-premise Client – AL (Read XML File)

Here, you can obtain a simple function that retrieves the server settings for the relevant on-premises web client instance. The Codeunit logic reads the CustomSettings.config file located in the application path of the applicable service instance. It can retrieve any setting value relevant to the key name, such as the database server, database instance name, database name, server instance name, client service port, etc. Here, you can get an idea of how to read an XML file in AL language. Here is the function:

procedure GetServerSettings(Keys: List of [Text]) Values: List of [Text]
    var
        ConfigFilePath: Text;
        XmlTextReader: DotNet XmlTextReader;
        XmlNodeType: DotNet XmlNodeType;
        AttributeName: Text;
        AttributeValue: Text;
        Index, i : Integer;
        KeysCount: Integer;
        AssignedValuesCount: Integer;
        KeyValue: Text;
    begin
        KeysCount := Keys.Count;
        if KeysCount = 0 then
            exit;
        foreach KeyValue in Keys do
            Values.Insert(Keys.IndexOf(KeyValue), '');
        ConfigFilePath := System.ApplicationPath() + 'CustomSettings.config';
        XmlTextReader := XmlTextReader.XmlTextReader(ConfigFilePath);
        while XmlTextReader.Read() do begin
            if XmlTextReader.NodeType = XmlNodeType.Element then begin
                if XmlTextReader.HasAttributes then BEGIN
                    while XmlTextReader.MoveToNextAttribute() do begin
                        AttributeName := XmlTextReader.Name;
                        AttributeValue := XmlTextReader.Value;
                        if AttributeName = 'key' then BEGIN
                            Index := Keys.IndexOf(AttributeValue);
                            if Index <> 0 then begin
                                Values.Set(Index, XmlTextReader.GetAttribute('value'));
                                AssignedValuesCount += 1;
                                if AssignedValuesCount = KeysCount then
                                    exit;
                            end;
                        end;
                    end;
                end;
            end;
        end;
        XmlTextReader.Close();
    end;Code language: PHP (php)

How to use the GetServerSetting function

You can pass multiple keys (key name of the setting) for which you want to retrieve values using a list, and it returns a list of values corresponding to the keys. Here is an example of the function.

local procedure TestGetServerSettings()
    var
        Keys: List of [Text];
        Values: List of [Text];
    begin
        Keys.Add('ServerInstance');
        Keys.Add('ClientServicesPort');
        Keys.Add('DatabaseServer');
        Keys.Add('DatabaseInstance');
        Keys.Add('DatabaseName');
        Values := GetServerSettings(Keys);
        Message(Values.Get(1));
        Message(Values.Get(2));
        Message(Values.Get(3));
        Message(Values.Get(4));
        Message(Values.Get(5));
    end;Code language: PHP (php)

Or you can use a loop to retrieve values as below:

local procedure TestGetServerSettings()
    var
        Keys: List of [Text];
        Values: List of [Text];
        Value: Text;
    begin
        Keys.Add('ServerInstance');
        Keys.Add('ClientServicesPort');
        Keys.Add('DatabaseServer');
        Keys.Add('DatabaseInstance');
        Keys.Add('DatabaseName');
        Values := GetServerSettings(Keys);
        foreach Value in Values do begin
            Message(Value);
        end;
    end;Code language: PHP (php)

Output:

BC230
7085
LAPTOP-A63CT72G
BCDEMO
Demo Database BC (23-0)

CustomSettings.config Business Central Server - Server Settings

You can retrieve any server setting relevant to the BC client, which can be very useful when doing integrations.

Visit the previous guide about how to send emails with attachments in AL.

Senior Solutions Architect - Microsoft Dynamics Navision / Microsoft Dynamics 365 Business Central and freelance developer. (The admin of NAVUSER)

Leave a Comment