/// <summary>
        /// Gets the process data fields
        /// </summary>
        /// <param name="processinstanceId"></param>
        /// <returns></returns>
        public List <dataField> GetProcessDataFields(int processinstanceId)
        {
            List <dataField> list = new List <dataField>();

            SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
            K2Conn.Open(servername);
            try
            {
                SourceCode.Workflow.Client.ProcessInstance K2Proc = K2Conn.OpenProcessInstance(processinstanceId);
                foreach (SourceCode.Workflow.Client.DataField datafield in K2Proc.DataFields)
                {
                    list.Add(new dataField {
                        Name      = datafield.Name,
                        Value     = datafield.Value.ToString(),
                        valueType = datafield.ValueType.ToString()
                    });
                }
            }
            catch (Exception ex)
            {
                list.Add(new dataField
                {
                    Name  = "Error",
                    Value = ex.Message.ToString()
                });
            }
            finally {
                K2Conn.Close();
            }

            return(list);
        }
        /// <summary>
        /// Starts a workflow
        /// </summary>
        /// <param name="Folio"></param>
        /// <param name="ProcessName"></param>
        /// <returns></returns>
        public int StartProcess(string Folio, string ProcessName)
        {
            var con = ConfigurationManager.AppSettings;


            int ProcessInstanceId = 0;

            SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
            K2Conn.Open(servername);
            try
            {
                SourceCode.Workflow.Client.ProcessInstance K2Proc = K2Conn.CreateProcessInstance(ProcessName);

                K2Proc.Folio = Folio;
                K2Conn.ImpersonateUser(ServiceAccount);
                K2Conn.StartProcessInstance(K2Proc);
                ProcessInstanceId = K2Proc.ID;
            }
            catch (Exception EX)
            {
                ProcessInstanceId = 0;
            }

            finally
            {
                K2Conn.Close();
            }
            return(ProcessInstanceId);
        }
        /// <summary>
        /// Task Details
        /// </summary>
        /// <param name="serialnumber"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public List <TaskDetails> OpenTask(string serialnumber, string destination)
        {
            List <TaskDetails> list = new List <TaskDetails>();

            SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
            K2Conn.Open(servername);
            K2Conn.ImpersonateUser(destination);
            SourceCode.Workflow.Client.WorklistItem K2WListItem = K2Conn.OpenWorklistItem(serialnumber);

            try
            {
                list.Add(new TaskDetails
                {
                    Status       = K2WListItem.Status.ToString(),
                    SerialNumber = K2WListItem.SerialNumber,
                    Actions      = K2WListItem.Actions,
                    Data         = K2WListItem.Data,
                    DataFields   = K2WListItem.ProcessInstance.DataFields.ToString()
                });


                K2WListItem.Release();
            }
            catch (Exception ex)
            {
                list.Add(new TaskDetails
                {
                    Status = "No Task"
                });
            }
            finally {
                K2Conn.Close();
            }
            return(list);
        }
        /// <summary>
        /// Actions a task
        /// </summary>
        /// <param name="action"></param>
        /// <param name="serialnumber"></param>
        /// <param name="destinationuser"></param>
        /// <returns></returns>
        public Boolean ActionTask(string action, string serialnumber, string destinationuser)
        {
            Boolean result = false;

            SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
            K2Conn.Open(servername);
            K2Conn.ImpersonateUser(destinationuser);
            SourceCode.Workflow.Client.WorklistItem K2WListItem = K2Conn.OpenWorklistItem(serialnumber);
            try
            {
                K2WListItem.Actions[action].Execute();
                result = true;
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally {
                K2Conn.Close();
            }
            return(result);
        }
        /// <summary>
        /// Gets the current status of the workflow
        /// </summary>
        /// <param name="processinstanceId"></param>
        /// <returns></returns>
        public string GetWorkflowStatus(int processinstanceId)
        {
            string Result = string.Empty;

            SourceCode.Workflow.Client.Connection K2Conn = new SourceCode.Workflow.Client.Connection();
            K2Conn.Open(servername);
            try
            {
                SourceCode.Workflow.Client.ProcessInstance K2Proc = K2Conn.OpenProcessInstance(processinstanceId);


                switch (K2Proc.Status1)
                {
                case SourceCode.Workflow.Client.ProcessInstance.Status.Active:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.Active.ToString();
                    break;
                }

                case SourceCode.Workflow.Client.ProcessInstance.Status.Completed:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.Completed.ToString();
                    break;
                }

                case SourceCode.Workflow.Client.ProcessInstance.Status.Deleted:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.Deleted.ToString();
                    break;
                }

                case SourceCode.Workflow.Client.ProcessInstance.Status.Error:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.Error.ToString();
                    break;
                }

                case SourceCode.Workflow.Client.ProcessInstance.Status.New:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.New.ToString();
                    break;
                }

                case SourceCode.Workflow.Client.ProcessInstance.Status.Running:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.Running.ToString();
                    break;
                }

                case SourceCode.Workflow.Client.ProcessInstance.Status.Stopped:
                {
                    Result = SourceCode.Workflow.Client.ProcessInstance.Status.Stopped.ToString();
                    break;
                }
                }
            }
            catch (Exception ex)
            { Result = ex.Message; }
            finally
            {
                K2Conn.Close();
            }

            return(Result);
        }