Here is the code to retrieve the worklist, and I wasn't the original author so still learning k2:
I believe the version we are running is 4.8210.2.0, i hope that is what you are looking for.
public string GetWorklists(string login)
       {
           try
           {
               List<K2Worklist> workLists = new List<K2Worklist>();
               //Create your connection object
               using (Connection K2Con = new Connection())
               {
                   ConnectionSetup K2ConSetup = new ConnectionSetup();
                   K2ConSetup.ConnectionParameters.Add(ConnectionSetup.ParamKeys.Host, ConfigurationManager.AppSettingsÂ"K2Server"]);
                   K2ConSetup.ConnectionParameters.Add(ConnectionSetup.ParamKeys.TimeOut, ConfigurationManager.AppSettingsr"K2ConnTimeOut"]);
                   //Open a connection to the K2.blackpearl] server
                   K2Con.Open(K2ConSetup);
                   K2Con.ImpersonateUser(login);
                   Worklist K2WorkList = K2Con.OpenWorklist("ASP");
                   K2Con.RevertUser();
                   foreach (WorklistItem K2WorkListItem in K2WorkList)
                   {
                       if (K2WorkListItem.Status == WorklistStatus.Allocated)
                       {
                           continue;
                       }
                       workLists.Add(ConvertWorkListItem(K2WorkListItem, login));
                   }
               }
               return new K2ServiceWorklistResult(true, string.Empty, null, workLists).ToString();
           }
           catch (Exception ex)
           {
               string error = string.Format("An exception occurred while getting the worklists for the given user. Login: {0}, Ex: {1}"
                   , login, ex.Message);
               System.Diagnostics.Trace.TraceError(error);
               return new K2ServiceWorklistResult(false, error, ex, null).ToString();
           }
       }
Â
private K2Worklist ConvertWorkListItem(WorklistItem item, string login)
       {
           K2Worklist workList = new K2Worklist();
           foreach (SourceCode.Workflow.Client.Action action in item.Actions)
           {
               workList.Actions.Add(action.Name);
           }
           workList.ProcessName = item.ProcessInstance.Name.ToString();
           workList.Activity = item.ActivityInstanceDestination.Name.ToString();
           workList.SerialNumber = item.SerialNumber.Replace(",", "_");
           XmlDocument SPIntegrationData = new XmlDocument();
           string processInstance = string.Empty;
           //Pull fields from K2 SPIntegrationData object
           foreach (XmlField xmlField in item.ProcessInstance.XmlFields)
           {
               if (xmlField.Name == "SPIntegrationData")
               {
                   processInstance = xmlField.Value;
               }
           }
           if (!string.IsNullOrEmpty(processInstance)) // it's a SharePoint item
           {
               SPIntegrationData.LoadXml(processInstance);
               string siteURL = SPIntegrationData.SelectSingleNode("/SPData/SiteURL").InnerText;
               string listURL = SPIntegrationData.SelectSingleNode("/SPData/ListURL").InnerText;
               string itemName = SPIntegrationData.SelectSingleNode("/SPData/ItemName").InnerText;
               string itemID = SPIntegrationData.SelectSingleNode("/SPData/ItemId").InnerText;
               string listID = SPIntegrationData.SelectSingleNode("/SPData/ListId").InnerText.Replace("-", "%2D");
               string webURL = SPIntegrationData.SelectSingleNode("/SPData/WebURL").InnerText;
               string workflowID = SPIntegrationData.SelectSingleNode("/SPData/WorkflowId").InnerText.Replace("-", "%2D");
               workList.Document = itemName;
               workList.DocumentLink = siteURL + listURL + "/" + itemName;
               workList.Properties = siteURL + listURL + "/Forms/MaintForm.aspx?ID=" + itemID + "&RootFolder=" + listURL;
               workList.PropertiesLinkText = "Edit Properties";
               workList.ActionUrl = webURL + "/_layouts/WrkStat.aspx?List=" + listID + "&WorkFlowInstanceID=" + workflowID;
               workList.ActionsLinkText = "Take Action";
           }
           else
           {
               // all other items
               workList.Document = item.ProcessInstance.Folio;
               workList.DocumentLink = item.Data.ToString();
               workList.Properties = string.Empty;
               workList.PropertiesLinkText = string.Empty;
               workList.ActionUrl = string.Empty;
               workList.ActionsLinkText = string.Empty;
           }
           foreach (SourceCode.Workflow.Client.DataField data in item.ProcessInstance.DataFields)
           {
               try
               {
                   workList.DataFields.Add(new Hcmlp.Shared.Service.K2Objects.K2DataField(data.Name, data.Value != null ? data.Value.ToString() : string.Empty));
               }
               catch (Exception ex)
               {
                   string error = "Error reading worklist data fields. Field Name: {0} Worklist folio: {1} Login: {2} Error: {3}"; ;
                   System.Diagnostics.Trace.TraceError(string.Format(error, data.Name, item.ProcessInstance.Folio, login, ex.Message));
                   continue;
               }
           }
           foreach (XmlField xmlField in item.ProcessInstance.XmlFields)
           {
               workList.DataFields.Add(new Hcmlp.Shared.Service.K2Objects.K2DataField(xmlField.Name, xmlField.Value));
           }
           return workList;
       }
There's a lot of XML manipulation which can be intensive, but apart from that it seems fairly OK (without going into the K2Worklist object).
Two things you can try:
- Close the K2Conn object before it goes out of scope. If you are working in a high volume environment, the garbage collector may not close the connections fast enough and you can face connection issues.Â
I.e. K2Conn.Close(); // Before the return call in the GetWorklists method
- Make sure that you really require all datafields to be displayed to the user. If you can eliminate some of those with large amounts of data (setting the On Demand flag) it might reduce overheads.