Fetch the value of Datafield

  • 2 December 2011
  • 4 replies
  • 1 view

Badge +2

Hi,


Can anyone please help me how to fetch the value of data fields of the given workitem in K2 through CSharp API.


Kindly do the needful.


Regards,


-Balaji. S


4 replies

Badge +10

Here is an example from the K2 online help:


Developer Reference > Processes > Accessing > How to retrieve data fields


 
using System;
using System.Collections.Generic;
using System.Text;
using SourceCode.Hosting.Client;
using SourceCode.Workflow.Client;
 
namespace K2Samples
{
    class WorkflowAccessingSample
    {
        public void RetrieveDataFields()
        {
            // TODO: Replace these placeholder values with values for your environment
            string _serverName = "blackpearl";
            string _user = "K2Student";
            string _domain = "DENALLIX";
            string _password = "K2pass!";
            string _wfSerialNumber = "4_12";
 
            SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder connectionString =
                new SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder();
 
            connectionString.Authenticate = true;
            connectionString.Host = "localhost";
            connectionString.Integrated = true;
            connectionString.IsPrimaryLogin = true;
            connectionString.Port = 5252;
            connectionString.UserID = _user;
            connectionString.WindowsDomain = _domain;
            connectionString.Password = _password;
            connectionString.SecurityLabelName = "K2"; //the default label
 
            // open a K2 connection
            Connection connection = new Connection();
            connection.Open(_serverName, connectionString.ToString());
 
            try
            {
                // loop through each worklist item
                foreach (WorklistItem worklistItem in connection.OpenWorklist())
                {
                    // open the worklist item
                    worklistItem.Open();
 
                    // match with the workflow serial number
                    if (worklistItem != null && worklistItem.SerialNumber == _wfSerialNumber)
                    {
                        // loop through each data field and write the name and value to the console
                        foreach (DataField dataField in worklistItem.ProcessInstance.DataFields)
                        {
                            Console.WriteLine(dataField.Name + ": " + dataField.Value);
                            Console.ReadLine();
                        }
                    }
                }
               
            }
            catch (Exception ex)
            {
                // write error to console
                Console.WriteLine("Error: " + ex.Message);
                Console.ReadLine();
            }
 
            finally
            {
                // close the connection
                connection.Close();
            }
        }
    }
}

Badge +2

Hi Tim,


Good to see your reply. Thank you very much. Definitely this will solve my issue. I have one more request. Your code seems to get the entire data field for the list of work item.


My scenario is, I have only one Process Instance object, holds only one work item. Is it possible to read that particular data field by passing the data field name and get the value of the same? Please help me for that also. And one more think where I can see the online help which you mention at the top of your reply. If you share the URL for the same then that will be very helpful for me.


 


Regards,


-Balaji. S

Badge +10

I modified a section of the sample so that it will only return a specific process datafield value for a specific worklist item. 



if (worklistItem != null && worklistItem.SerialNumber == _wfSerialNumber)
{
       Console.WriteLine(worklistItem.ProcessInstance.DataFields["Name of my Process Datafield"].Value);
}


The sample I posted comes with the help that ships directly with K2 software and is installed by default with the client tools (I guess I shouldn't have said online help although the k2 blackpearl documentation is also available at help.k2.com).  From the Start/All Programs/K2 blackpearl select K2 blackpearl Documentation you can follow the topic sections to find the code sample I provided and many more. 

Badge +2

Hi,


Thank you very much.


Regards,


-Balaji. S

Reply