How to get servername in RTC

reijermolenaarreijermolenaar Member Posts: 256
edited 2011-08-30 in NAV Three Tier
Hi all,

Does anybody know how to retrieve the name of the SQL server in the RTC?
Both CONTEXTURL and virtual table "2000000047 - Server" are not available...
Reijer Molenaar
Object Manager

Answers

  • ara3nara3n Member Posts: 9,256
    I don't know if there is an easy way. But the following solution would work.

    Create(MSDOM);
    MSDOM.load(APPLICATIONPATH+ 'CustomSettings.config');

    MSNode := MSDOM.SelectSingleNode('// Location of DatabaseServer key');

    Message(MSNode.Value);
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • reijermolenaarreijermolenaar Member Posts: 256
    Thanks Rashed!
    It works indeed: \:D/
    IF ISSERVICETIER THEN BEGIN
    
      IF ISCLEAR(DomDoc) THEN
        CREATE(DomDoc);
    
      DomDoc.load(APPLICATIONPATH + 'CustomSettings.config');
      DomNode := DomDoc.selectSingleNode('//appSettings/add[@key=''DatabaseServer'']');
      ExitValue := DomNode.attributes.item(1).text;
    
      CLEAR(DomDoc);
    
    END ELSE BEGIN
    
      AllObj.SETRANGE("Object Type", AllObj."Object Type"::Table);
      AllObj.SETRANGE("Object ID", 2000000047);
    
      IF AllObj.FINDFIRST THEN BEGIN
    
        // SQL
        RecRef.OPEN(2000000047);
        FldRef := RecRef.FIELD(2);
        FldRef.SETRANGE(TRUE);
        RecRef.FINDFIRST; 
        FldRef := RecRef.FIELD(1);
        ExitValue := FORMAT(FldRef.VALUE);
        RecRef.CLOSE;
    
      END;
    
    END;
    
    Reijer Molenaar
    Object Manager
  • afarrafarr Member Posts: 287
    Thanks a lot - just what I needed today. I would have spent a couple of hours (at least) figuring it out otherwise.
    Alastair Farrugia
  • ara3nara3n Member Posts: 9,256
    You are welcome. Although reijermolenaar provided the full solution. So thanks reijermolenaar for fully working solution.
    All I did was guide him to the right path.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • BeliasBelias Member Posts: 2,998
    thanks a lot, it was useful for me, too! i was trying to tweak a report by putting my code in strange places, but it didn't work :whistle:
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • ColloCollo Member Posts: 12
    Hi

    I am still having a problem with this.
    The posted solution only works if the application server is on the same server as the SQL.
    In a 3 teir environment I still cannot determine the applcation server.

    Any ideas anyone?
  • ara3nara3n Member Posts: 9,256
    try and debug the service tier.
    Ahmed Rashed Amini
    Independent Consultant/Developer


    blog: https://dynamicsuser.net/nav/b/ara3n
  • afarrafarr Member Posts: 287
    The original question was about the SQL Server.
    If you want the middle tier, you could try replacing "DatabaseServer" with "ServerInstance".

    If you want the full server name, e.g. MiddleMachine:7046/DynamicsNAV, then I wouldn't know how to get that, but there must be some way of getting that, as you can see it (and change it) in RTC.
    Alastair Farrugia
  • deV.chdeV.ch Member Posts: 543
    To get the full server name port and instance name:
    Name	DataType	Subtype	Length
    DomDoc	Automation	'Microsoft XML, v3.0'.DOMDocument	
    DomNode	Automation	'Microsoft XML, v3.0'.IXMLDOMNode	
    WSHNetwork	Automation	'Windows Script Host Object Model'.WshNetwork
    
    GetServerAndServiceName(VAR _ServerTxt : Text[30];VAR _PortTxt : Text[30];VAR _ServiceTxt : Text[30])
    IF ISCLEAR(WSHNetwork) THEN
      CREATE(WSHNetwork);
    
    IF ISCLEAR(DomDoc) THEN
      CREATE(DomDoc);
    
    _ServerTxt := WSHNetwork.ComputerName();
    
    DomDoc.load(APPLICATIONPATH + 'CustomSettings.config');
    
    DomNode := DomDoc.selectSingleNode('//appSettings/add[@key=''ServerPort'']');
    IF NOT ISCLEAR(DomNode) THEN
      _PortTxt := DomNode.attributes.item(1).text; // Server Port
    
    DomNode := DomDoc.selectSingleNode('//appSettings/add[@key=''ServerInstance'']');
    IF NOT ISCLEAR(DomNode) THEN
      _ServiceTxt := DomNode.attributes.item(1).text; // Server Name
    
    CLEAR(DomDoc);
    Clear(WSHNetwork);
    
    
  • ColloCollo Member Posts: 12
    Brilliant..

    Thanks deV.ch

    WSHNetwork.Computername was just what I needed.
    :P
  • Big_DBig_D Member Posts: 207
    Anybody got this to work in Nav 2015?
    I get "You can create a Automation Object .. on Microsoft Dynamics Nav Server. You must create it on a client computer.
    If my Sql Server Name is SQLServer\SQL2012, how do I get the full SQL Server Name in C/AL including the SQL2012 part?
    Thanks

    Big D signing off!
  • vaprogvaprog Member Posts: 1,140
    Just rewrite the Automation stuff using client side DotNet (System.Xml, not the MSXML through COM interoperability)

    Please note: The above code samples depend on you using the default connection.

    You may find other examples using server side code, but those usually cannot cope with service instances.

    You might be interested in an entirely different approach from Kauffmann @ Dynamics NAV: Read Server Settings from C/AL code, but this is undocumented and appears to be not stable across NAV versions.
  • Big_DBig_D Member Posts: 207
    Thanks vaprog that's a cool way of doing that - a piece of cake :) !
    Big D signing off!
Sign In or Register to comment.