To execute .NET code on NGD whenever an app is opened on an iOS, Android device, or web browser, use the contactServer() JavaScript API to send a command to the server from within the EH_AfterOpen event handler in JS. Using the contactServer() API at any point in the script can call requested data from the sever. The .NET form script event handler serverContacted() will process the command and return any data as needed. However, the device must have an internet connection for these APIs to function. 


In this example, see both JS form script and .NET script. The following occurs:


On JavaScript based Client:

1. EH_AfterOpen will run whenever form is opened.

2. EH_AfterOpen runs contactServer which sends a command "DateTimePlease" to the server.


On .NET based Server:

3. ServerContacted will accept the "DateTimePlease" command and return a server response accordingly.


On JavaScript based Client:

4. The success callback function of the contactServer API will accept the server response and display the results.


Although the example simply retrieves the current date and time, the server-side code could execute any function supported within the .NET framework (i.e. custom libraries, reaching out to an internal resource such as a database, etc.). 


JavaScript Code

// Triggered when the form is opened
function EH_AfterOpen() {
    
    // Set an initial message for the user
    _form.setValue("Response", "Sending a command to the server... please wait.");
    
    // Get ready to send a command to the server
    var sendSessionState = false;                // If true, we will send the entire data "state" of the form to the server... in most cases, not needed.
    var commandToSend = "DateTimePlease";        // This is the command to send to the .NET event handler "serverContacted".
        
    _form.contactServer(sendSessionState, commandToSend, 
        function (response){
            // Success
            
            // Note: we just receive a response from the server-side
            //         Now, we display it...
            _form.setValue("Response", response);
        },
        function (msg){
            // Fail
            _form.setValue("Response", "Not able to send a command to the server. Details: " + msg);
        }
    );
}


VB .NET Code

<MiCode(FormScriptEventType.ServerContacted)> _
    Public Sub Form_ServerContacted(ByVal e As ServerContactedEventArgs)
        ' We just got a command from the JS-side client...
       
        Select Case e.ServerCommand
            Case "DateTimePlease"
                ' Client is asking for date / time...
                ' (Note that code here could be querying from a database, webservice, etc... )
               
                Dim DTNow As DateTime = DateTime.Now()
               
                ' ServerResponse will be set and sent here...
                e.ServerResponse = DTNow.ToString("MM/dd/yyyy HH:mm:ss")
               
        End Select
    End Sub