Skip to main content
Here is a sample on how to implement an asynchronous server event in K2.net. The first block of code is from within the actual server event in K2.net Studio. This code will allow you to call the external system (such as a custom Finance system), and also inform the K2.net Server to wait for an external callback, before continuing with the process instance.


Sub Main(Byval K2 as ServerEventContext)
'The following line have code has been changed. By default, a server
'event is 'K2.Synchronous = True' which specifies that the server
'event is complete as soon as all the code has been executed.
'With K2.Synchronous = False, the Server will execute this code, and
'wait for another object model call, in order to complete the item.

K2.Synchronous = False

'Create a string variable
Dim mySerialNo As String

'Store the Unique ID of this Server Event in the variable
mySerialNo = K2.SerialNumber

'TODO: Call the External System:
'This could be the instantiation of an external application .
'Additionally, you need to store the string contained within 'mySerialNo'
'somewhere, because you will need this to complete this server event later.

End Sub


The following block of code can be written externally (such as in VS.NET). This code instantiates the K2.net Object Model (K2ROM) (which must be included as a reference), reinstantiates the server event item, sets any data (if required), and finishes the server event item.


'Retrieve the Serial Number. Note that this must be the serial number that was stored
'when the server event code was called.

Dim mySerialNo As String
mySerialNo = "12345"

'Open a new connection to the K2.net Server. In this example, I am
'inheriting the credentials from IIS. If you want to explicitly
'specify which user is connecting, then use this syntax:

'myConnection.Open("{myServerName}","domain,user,password")

'Example:
'myConnection.Open("OlafPC","k2demo,olaf,123")

Dim myConnection As New SourceCode.K2ROM.Connection
myConnection.Open("{myServerName}")

'Instantiate the Same Server Event Item Again. This is done by
'passing in the aforementioned Serial Number.

Dim myServerItem As SourceCode.K2ROM.ServerItem
myServerItem = myConnection.OpenServerItem(mySerialNo)

'Once you have a handle to the server event item, you can read/write
'any data fields again.

myServerItem.ProcessInstance.DataFields("Field1").Value = "1"

'In order to complete the server event, simply call the 'Finish' method.
'The K2.net Server will then continue with the process.

myServerItem.Finish()
Be the first to reply!

Reply