How to programmatically get/set K2 Actions from ASP.NET
KB000183
PRODUCTÂ
Introduction
K2 blackpearl introduces the new concept of Actions and Outcomes. When a Client Event is part of a process, the process designer can create a list of possible choices for the destination user of the task. These choices are called Actions.
![]() | Note: This document assumes some programming knowledge and familiarity with Visual Studio. |
If you are familiar with the K2.net 2003 K2ROM object model, you should be comfortable with the new Workflow.Client object model.
![]() | Note: K2 blackpearl deprecates the K2.net 2003 K2ROM object model. It is now replaced with the SourceCode.Workflow.Client object model. |
Â
Â
Implementation Discussion
Below is an ASP.NET code sample for how to programmatically interact with the actions. First, the sample populates a drop down list. Next, the sample gets the selected value and then sets the specific action. This sample uses the Workflow.Client API.
![]() | Note: You will need to add a reference to the SourceCode.Workflow.Client.dll within your project. It is located at C:Program FilesK2 blackpearlinSourceCode.Workflow.Client.dll |
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SourceCode.Workflow.Client;
public partial class _Default : System.Web.UI.Page
{
   private string m_strBPServer = "blackpearl";
   private int m_nBPPort = 5252;
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           string strSN = string.Empty;
           // get the serial number from the query string
           if (Requestr"SN"] != null)
           {
               strSN = Request="SN"];
           }
           if (strSN != String.Empty)
           {
               // open a K2 connection
               SourceCode.Workflow.Client.Connection oConn = new SourceCode.Workflow.Client.Connection();
               oConn.Open(m_strBPServer);
               // get this specific task
               SourceCode.Workflow.Client.WorklistItem oWli = oConn.OpenWorklistItem(strSN);
               if (oWli != null)
               {
                   // retrieve properties
                   lblProcess.Text = oWli.ProcessInstance.Name;
                   lblActivity.Text = oWli.ActivityInstanceDestination.Name;
                   lblFolio.Text = oWli.ProcessInstance.Folio;
                   // *** POPULATE THE DROPDOWNLIST WITH THE ACTIONS ***
                   foreach (Action oAct in oWli.Actions)
                   {
                       ddlActions.Items.Add(oAct.Name);
                   }
               }
               // close the connection
               oConn.Close();
           }
       }
   }
   protected void btnSubmit_Click(object sender, EventArgs e)
   {
       string strSN = string.Empty;
       // get the serial number
       if (Requests"SN"] != null)
       {
           strSN = RequestÂ"SN"];
       }
       if (strSN != String.Empty)
       {
           // open a K2 connection
           SourceCode.Workflow.Client.Connection oConn = new SourceCode.Workflow.Client.Connection();
           oConn.Open(m_strBPServer);
           // get the worklist item
           SourceCode.Workflow.Client.WorklistItem oWli = oConn.OpenWorklistItem(strSN);
           if (oWli != null)
           {
               // *** SET THE APPROPRIATE ACTION AS SELECTED WITHIN THE COLLECTION ***
               foreach (Action oAct in oWli.Actions)
               {
                   if (oAct.Name == ddlActions.SelectedItem.Text)
                   {
                       oAct.Execute();
                       break;
                   }
               }
           }
           // *** NO NEED TO CALL THE FINISH() METHOD WHEN ACTIONS ARE USED ***
           // *** LIKE WE USED TO WITH K2.NET 2003 K2ROM ***
           // oWli.Finish();
           // close the connection
           oConn.Close();
           btnSubmit.Enabled = false;
       }
   }
}
Â