Creating and Starting Sessions
- Last UpdatedNov 06, 2025
- 2 minute read
The key to writing script for MES is the session. This topic explains the following:
- Creating Sessions
- Starting Sessions
To use any functionality that modifies records in the MES database with the stateless API, you must first create a session.
Unlike the stateful API, sessions are optional in the stateless API calls. It is possible to have no session when making a call that queries information from the database, to share sessions with another application, and to have multiple sessions within an application. The reason for using a valid session is to log the user information into the database. If an invalid session ID is passed or no user is logged in, then the transactions will still occur (assuming no other errors) with the edited_by field set to the database connection user of the Middleware.
To locate an existing session, the aaMES.Core.aaSession.GetSessions() method call retrieves a list of existing sessions from the sessn table based on the filters provided. The aaMES.Core.aaSession.StartSession() method is available to start a new session. Multiple sessions can also be created. When using a session from the database, select one of the type clientOperator (ID 37) and not a session from one of the objects (OCO, UCO, or SRO).
For an example, see the sample script:
Sample Script for Creating Sessions (VB.NET)
dim SessionID as Integer
Dim Result as aaMES.Result;
dim oNull as Object;
dim userID as string;
dim DS as System.Data.DataSet;
dim ClientType as Object;
dim test as integer;
'ClientType = 37;
test = aaMES.Core.aaClientTypes.clientOperator;
ClientType = test;
if (me.Initialize.ExecutionCnt > 2) then
if me.debug then
LogMessage("Creating client session");
endif;
'aaMES.Core.aaSession.GetSessions( sessionId, clientType, userId );
Result = aaMES.Core.aaSession.GetSessions( oNull, ClientType, userId );
DS = Result.Dataset_Value;
if (DS.Tables(0).Rows.count > 0) then
'use an existing connection
me.sessionID = DS.Tables(0).Rows(0).item("session_id");
me.MES.UserID = DS.Tables(0).Rows(0).item("user_id");
else
' start client operator session
'aaMES.Core.aaSession.StartSession( clientType, sessionId );
Result = aaMES.Core.aaSession.StartSession( aaMES.Core.aaclientTypes.ClientOperator, sessionId );
me.SessionID = SessionID; if me.sessionID > 0 then
if me.debug then
LogMessage("Session Started");
endif;
else
me.resultText = "Session FAILED to start: " + result.Exception.Message;
endif;
endif;
me.initializationComplete = true;
endif;