Using K2 REST Services to show Worklist Items in a ASP.NET application
Using the K2 REST services provides a developer with the ability to start new process instances, access process instances data and XML fields, retrieve and action items from a user’s K2 worklist, search for K2 users, and access and update a user’s tasks items. These functions are URI based, and can therefore be accessed using standard HTTP GET and POST requests in the code file portion of an ASP.NET application.
Retrieving And Displaying Worklist Item Information
The following project demonstrates the use of the Worklist/Items REST service. It will retrieve all the user’s worklist items and then display the first item using language integrated query (LINQ) data structures to locate the data in the returned XML data stream:
Default.aspx:
<%@PageLanguage="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestASP_Web_Application_for_REST._Default" %> <!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="getDataButton" runat="server" onclick="getDataButton_Click" Text="Get Worklist Data" /> <br/> <table style="width: 100%;"> <tr> <td> Process Name: </td> <td> <asp:TextBox ID="processName" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Serial Number: </td> <td> <asp:TextBox ID="serialNumber" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Alloocated User: </td> <td> <asp:TextBox ID="allocatedUser" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Process DataField Value: </td> <td> <asp:TextBox ID="procDataField" unat="server"></asp:TextBox> </td> </tr> <tr> <td> Activity DataField Value: </td> <td> <asp:TextBox ID="actDataField" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Available Actions: </td> <td> <asp:DropDownList ID="actionsDropDownList" runat="server" Width="129px"> </asp:DropDownList> </td> </tr> </table> </div> </form> </body> </html>
Default.aspx.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.Xml.Linq; using System.Xml; using System.IO; namespace TestASP_Web_Application_for_REST { public partial class _Default : System.Web.UI.Page { // Change the RestURL to match your workspace environment
private const string RestUrl = "http://sav-docs:81/K2Services/REST.svc/Worklist/Items?piDataField=true&actXmlField=true"; private static readonly XNamespace WorklistNamespace = "http://schemas.k2.com/worklist/d1"; private static readonly XNamespace ActivityNamespace = "http://schemas.k2.com/activity/d1"; private static readonly XNamespace ProcessNamespace = "http://schemas.k2.com/process/d1"; private static readonly XNamespace EventNamespace = "http://schemas.k2.com/event/d1"; protected void Page_Load(object sender, EventArgs e) { } protected void getDataButton_Click(object sender, EventArgs e) { GetData(); } private void GetData() { // create the web request
HttpWebRequest request = WebRequest.Create(RestUrl) as HttpWebRequest; // Assign Credentials request.Credentials = CredentialCache.DefaultCredentials; // get response
XDocument responseDocument; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) using (Stream responseStream = response.GetResponseStream()) using (XmlReader responseReader = XmlReader.Create(responseStream)) { responseDocument = XDocument.Load(responseReader); } //Call method to extract a specific value
PopulateAllocatedUserTextBox(responseDocument); PopulateActivityDataTextBox(responseDocument); PopulateProcessDataTextBox(responseDocument); PopulateWorklistItemSerialNumberTextBox(responseDocument); PopulateProcessNameTextBox(responseDocument); PopulateActions(responseDocument); } private void PopulateActions(XDocument responseDocument) { var actionsQuery = from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection") from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem") from action in worklistItem.Elements(WorklistNamespace + "Action") select new { SerialNumber = (string)worklistItem.Attribute("SerialNumber"), Name = (string)action.Attribute("Name"), Batchable = (bool)action.Attribute("Batchable") }; foreach (var action in actionsQuery) { var listItem = new ListItem() { Text = action.Name, Value = action.SerialNumber, Enabled = action.Batchable, }; string thisSerialNumber = listItem.Value.ToString(); if (thisSerialNumber == serialNumber.Text) { actionsDropDownList.Items.Add(listItem); } } } private void PopulateProcessDataTextBox(XDocument responseDocument) { var dataFieldValueQuery = from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection") from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem") from processInstance in worklistItem.Elements(ProcessNamespace + "ProcessInstance") from dataField in processInstance.Elements(ProcessNamespace + "DataField") where (string)dataField.Attribute("Name") == "MyProcessDataField" select (string)dataField; var firstResult = dataFieldValueQuery.FirstOrDefault(); procDataField.Text = firstResult; } private void PopulateActivityDataTextBox(XDocument responseDocument) { var dataFieldValueQuery = from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection") from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem") from activityInstanceDestination in worklistItem.Elements(ActivityNamespace + "ActivityInstanceDestination") from dataField in activityInstanceDestination.Elements(ProcessNamespace + "DataField") where (string)dataField.Attribute("Name") == "MyActivityDataField" select (string)dataField; var firstResult = dataFieldValueQuery.FirstOrDefault(); actDataField.Text = firstResult; } private void PopulateAllocatedUserTextBox(XDocument responseDocument) { var allocatedUserFieldValueQuery = from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection") from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem") select (string)worklistItem.Attribute("AllocatedUser").Value; var firstResult = allocatedUserFieldValueQuery.FirstOrDefault(); allocatedUser.Text = firstResult; } private void PopulateProcessNameTextBox(XDocument responseDocument) { var processFullNameFieldValueQuery = from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection") from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem") from processInstance in worklistItem.Elements(ProcessNamespace + "ProcessInstance") select (string)processInstance.Attribute("FullName").Value; var firstResult = processFullNameFieldValueQuery.FirstOrDefault(); processName.Text = firstResult; } private void PopulateWorklistItemSerialNumberTextBox(XDocument responseDocument) { var processFullNameFieldValueQuery = from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection") from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem") select (string)worklistItem.Attribute("SerialNumber").Value; var firstResult = processFullNameFieldValueQuery.FirstOrDefault(); serialNumber.Text = firstResult; } } }