Skip to main content
Scenario:
The idea around it is to get K2.net to determine the navigation of a website, this can typically be used where a user needs to complete various sections of information that does not fit on one webpage.

This example is based on a number of forms:

Start - Start Page
Form1 Client Page
Form2 Client Page
Form3 Client Page

The process design is illustrated in the image below.

A couple of notes about the process design:
1. Destination on each Activity is set to Originator
2. Each Activity contains a data field called Direction , it is up to the lines to determine the direction of the workflow. The client pages will set a value to the data field based on the user input either by clicking Next of Back buttons on the form.
3. The standard code generated by the Client events has been modified slightly to change the Platform from ASP to Sync . The purpose of this is to filter the Worklist and insure that we get only work items related to this workflow back in the OpenWorklist calls. Please note that you can use any value as the platform as long as the same value is used in the OpenWorklist

Function HTTPFunction(Byval K2 as ClientEventContext)
Dim strURL as String
strURL = "http://localhost/SyncSample/Form1.aspx?sn={SERIALNO}"
strURL = strURL.Replace("{SERIALNO}",K2.SerialNumber)
'K2.AddWorklist("ASP",System.Web.HttpUtility.UrlPathEncode(strURL))
'change the Platform
K2.AddWorklist("Sync",System.Web.HttpUtility.UrlPathEncode(strURL))
End Function


Ok so let s have a look at the code required on the Worklfow UI s

Start Page:
cmdNext_Click - The following code is required on the Next button of the form

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Dim oCon As New SourceCode.K2ROM.Connection
Dim oProcInst As SourceCode.K2ROM.ProcessInstance
Dim oWorklist As SourceCode.K2ROM.Worklist
Dim oWorklistItem As SourceCode.K2ROM.WorklistItem
Dim intProcID As Int16
Dim strRedirectURL As String

Try
'start new process instance
oCon.Open("K2VPC")
oProcInst = oCon.CreateProcessInstance("Internal ProcessesSyncTest")
oProcInst.DataFields("Start").Value = txtInput.Text
oCon.StartProcessInstance(oProcInst, True)
intProcID = oProcInst.ID

'open worklist and get the next worklist item to determine the redirect url
oWorklist = oCon.OpenWorklist("Sync") 'TIP: update code in client event so that the platform is changed to Sync

For Each oWorklistItem In oWorklist
If oWorklistItem.ProcessInstance.ID = intProcID Then
'Response.Write("Het Hom")
strRedirectURL = oWorklistItem.Data
End If
Next
Response.Write("URL = " & strRedirectURL)
Catch ex As Exception
strRedirectURL = "http://localhost/SyncSample/ErrorPage.aspx"
Finally
oCon.Close()
Response.Redirect(strRedirectURL)
End Try
End Sub


PLEASE NOTE: oCon.StartProcessInstance(oProcInst, True) The Synchronous parameter must be True

Form1
Page_Load Open the Worklist Item and display the information on the form

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim oCon As New SourceCode.K2ROM.Connection
Dim oWorklist As SourceCode.K2ROM.Worklist
Dim oWorklistItem As SourceCode.K2ROM.WorklistItem
Dim strSerialNumber As String

Try
strSerialNumber = Request.QueryString("sn")
oCon.Open("K2VPC")
oWorklistItem = oCon.OpenWorklistItem(strSerialNumber, "Sync")
txtStartFormInput.Text = oWorklistItem.ProcessInstance.DataFields("Start").Value
Catch ex As Exception
Response.Redirect("http://localhost/SyncSample/ErrorPage.aspx")
Finally
oCon.Close()
End Try
End Sub


cmdNext_Click Update the data and Finish the Worklist item before redirecting to the next form


Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Dim oCon As New SourceCode.K2ROM.Connection
Dim oWorklist As SourceCode.K2ROM.Worklist
Dim oWorklistItem As SourceCode.K2ROM.WorklistItem
Dim strSerialNumber As String
Dim strRedirectURL As String
Dim intProcID As Int64
Dim arrTemp As Array


Try
strSerialNumber = Request.QueryString("sn")
oCon.Open("K2VPC")
oWorklistItem = oCon.OpenWorklistItem(strSerialNumber, "Sync")
oWorklistItem.ProcessInstance.DataFields("Form1").Value = txtInput.Text
oWorklistItem.ActivityInstanceDestination.DataFields("Direction").Value = "Next"
oWorklistItem.Finish(True)

'open worklist and get the next worklist item to determine the redirect url
oWorklist = oCon.OpenWorklist("Sync") 'TIP: update code in client event so that the platform is changed to Sync

'get process id from serial number
arrTemp = Split(strSerialNumber, ",")
intProcID = arrTemp(1)

For Each oWorklistItem In oWorklist
If oWorklistItem.ProcessInstance.ID = intProcID Then
strRedirectURL = oWorklistItem.Data
End If
Next
Catch ex As Exception
strRedirectURL = "http://localhost/SyncSample/ErrorPage.aspx"
Finally
oCon.Close()
Response.Redirect(strRedirectURL)
End Try
End Sub


PLEASE NOTE: oWorklistItem.Finish(True) The Synchronous parameter must be True


Form2
Note: The same code can be used in Page_Load and cmdNext_Click .

cmdBack_Click Allows the process to navigate to previous activity


Private Sub cmdBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBack.Click
Dim oCon As New SourceCode.K2ROM.Connection
Dim oWorklist As SourceCode.K2ROM.Worklist
Dim oWorklistItem As SourceCode.K2ROM.WorklistItem
Dim strSerialNumber As String
Dim strRedirectURL As String
Dim intProcID As Int64
Dim arrTemp As Array


Try
strSerialNumber = Request.QueryString("sn")
oCon.Open("K2VPC")
oWorklistItem = oCon.OpenWorklistItem(strSerialNumber, "Sync")
oWorklistItem.ProcessInstance.DataFields("Form2").Value = txtInput.Text
oWorklistItem.ActivityInstanceDestination.DataFields("Direction").Value = "Back"
oWorklistItem.Finish(True)

'open worklist and get the next worklist item to determine the redirect url
oWorklist = oCon.OpenWorklist("Sync") 'TIP: update code in client event so that the platform is changed to Sync

'get process id from serial number
arrTemp = Split(strSerialNumber, ",")
intProcID = arrTemp(1)

For Each oWorklistItem In oWorklist
If oWorklistItem.ProcessInstance.ID = intProcID Then
strRedirectURL = oWorklistItem.Data
End If
Next
Catch ex As Exception
strRedirectURL = "http://localhost/SyncSample/ErrorPage.aspx"
Finally
oCon.Close()
Response.Redirect(strRedirectURL)
End Try
End Sub


PLEASE NOTE: oWorklistItem.Finish(True) The Synchronous parameter must be True


Form3
Note: Use the same code as with previous form, however the code in the Next will look slightly different because this is the last form to be filled out by the user

cmdNext_Click Complete the Worklist Item and force the navigation to the last page


Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
Dim oCon As New SourceCode.K2ROM.Connection
Dim oWorklist As SourceCode.K2ROM.Worklist
Dim oWorklistItem As SourceCode.K2ROM.WorklistItem
Dim strSerialNumber As String
Dim strRedirectURL As String
Dim intProcID As Int64
Dim arrTemp As Array


Try
strSerialNumber = Request.QueryString("sn")
oCon.Open("K2VPC")
oWorklistItem = oCon.OpenWorklistItem(strSerialNumber, "Sync")
oWorklistItem.ProcessInstance.DataFields("Form3").Value = txtInput.Text
oWorklistItem.ActivityInstanceDestination.DataFields("Direction").Value = "Next"
oWorklistItem.Finish()
Catch ex As Exception
strRedirectURL = "http://localhost/SyncSample/ErrorPage.aspx"
Finally
oCon.Close()
Response.Redirect("http://localhost/SyncSample/Done.aspx")
End Try
End Sub


Let me know if this is useful!
The syncronous parameter is a really good thing to point out to people who may be new to the K2ROM. I have an application with a very similar workflow, that stores data in its own database but uses K2 for workflow. I was experiencing a "A database error occured" error when checking for the validity of a K2 serial number directly after updating some process data. My main validation function called the second.

I didn't have this error on the development server where K2 and the database were all on the same machine, but it happened in our staging environment which has K2 and the database on separate machines. Changing the sync parameter to true fixed the errors.

As an example of how to catch this kind of thing, here's my validation function:


protected bool CheckK2SerialNumber(string strSerialNumber)
{
bool bIsValid = true;
SourceCode.K2ROM.Connection K2Conn = new SourceCode.K2ROM.Connection();
try
{
K2Conn.Open(ConfigurationSettings.AppSettings.Get("PlanServer"));

SourceCode.K2ROM.WorklistItem wliCurrent;

wliCurrent = K2Conn.OpenWorklistItem(strSerialNumber, "ASP", false, true);

}
catch (System.Exception ex)
{
if (ex.Message.IndexOf("Invalid serialnumber") == 0)
{
bIsValid = false;
}
else if (ex.Message.IndexOf("Worklist item not found") == 0)
{
bIsValid = false;
}
else if (ex.Message.IndexOf("A database error occured") == 0)
{
bIsValid = false;
}
else
{
bIsValid = false;
ex = new Exception("Couldn't connect to K2 server.");
throw;
}
}
finally
{
K2Conn.Close();
K2Conn = null;
}
return bIsValid;
}


This code will have to be changed for SP4 of course (we don't have it yet) because the error messages have been slightly changed.

Reply