/// <summary> /// Method to check if a specified process instance still exists. /// </summary> /// <param name="procInstID">The process instance ID that needs to be checked.</param> /// <param name="server">An instance of the workflow management server.</param> /// <returns></returns> private bool ProcessInstanceExists(int procInstID, k2Mgnt.WorkflowManagementServer server) { // use SmartObjectClientServer for accessing SmartObjects SmartObjectClientServer sos = new SmartObjectClientServer(); // prepare the connection sos.CreateConnection(); // open the connection sos.Connection.Open(connectionString); // get a "Process Instance" SmartObject (an ootB K2 SmartObject > Workflow Reports > Workflow General) SmartObject so = sos.GetSmartObject("Process_Instance"); // tell the SmartObject to return a list of results so.MethodToExecute = "List"; // set filter for results - return the live instance for the specified procInstID so.Properties["ProcessInstanceID"].Value = procInstID.ToString(); // execute - return the results as a DataTable DataTable dt = sos.ExecuteListDataTable(so); // if there's one row the instance is still there and not deleted if (dt.Rows.Count == 1) { // close the connection and return true sos.Connection.Close(); return(true); } // if there is no row than the instance has been deleted return(false); }
/// <summary> /// 撤消任务 /// </summary> /// <param name="procInstID">流程实例ID,格式:1_2_3_4</param> /// <param name="activityName">实例结束结点</param> public static void CancelProcInstance(string procInstID) { Mang.WorkflowManagementServer svr = new Mang.WorkflowManagementServer(); try { svr.CreateConnection(); svr.Connection.Open(GetServerConnectionSetup()); string[] procInstIDs = procInstID.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries); foreach (string id in procInstIDs) { svr.GotoActivity(Convert.ToInt32(id), "结束"); } } catch { throw; } finally { if (svr != null) { svr.Connection.Close(); } } }
/// <summary> /// This is a sample code to set or remove the run as credentials in K2 Workspace /// http://codecodecode.ninja/2015/07/setting-event-run-as-rights-via-api/ /// /// K2 Article: /// http://help.k2.com/onlinehelp/K2blackpearl/UserGuide/current/webframe.html#Specifying_Credentials_in_K2_Workspace_What_to_do.html /// /// </summary> /// <param name="args"></param> static void Main(string[] args) { SCWFM.WorkflowManagementServer svr = null; try { // TODO. Set server name here. You need to run as a K2 Admin account svr = new SCWFM.WorkflowManagementServer("k2.myCoy.com", 5555); svr.Open(); Console.WriteLine("Is Current User Admin: {0}", svr.IsCurrentUserAdmin().ToString()); // TODO: Set process full path here SCWFM.ProcessSet procSet = svr.GetProcSet(@"TestEmailProj\Process1"); SCWFM.Processes processes = svr.GetProcessVersions(procSet.ProcSetID); foreach (SCWFM.Process proc in processes) { //NOTE: this will be looking for the current default process version only. if (proc.DefaultVersion) { SCWFM.Activities activities = svr.GetProcActivities(proc.ProcID); foreach (SCWFM.Activity activity in activities) { //TODO: set the activity name here. You can improvise here to loop from config files if (activity.Name.Equals("DefaultActivity2", StringComparison.OrdinalIgnoreCase)) { SCWFM.Events evtList = svr.GetActivityEvents(activity.ID); foreach (SCWFM.Event evt in evtList) { //TODO: set the event name you are looking for. if (evt.Name.Equals("Rejected", StringComparison.OrdinalIgnoreCase)) { //TODO: Set the user's credential here bool result = svr.SetRunAsUser(@"myCoy\test1", "pass@word1", "K2", evt.Code, proc.ProcID); Console.WriteLine("Set user on {0}({1}), {2}({3}) = {4}", activity.Name, proc.ProcID, evt.Name, evt.Code.ToString(), result); //NOTE: use this to remove the credential and revert to service account //bool result = svr.DeleteRunAsUser(evt.Code, proc.ProcID); //Console.WriteLine("Reset to service acctount on {0}({1}), {2}({3}) = {4}", activity.Name, proc.ProcID, evt.Name, evt.Code.ToString(), result); } } } } } } } catch (Exception ex) { Console.WriteLine("Exeception: {0}", ex.Message); Console.WriteLine(ex.StackTrace); } finally { if (svr.Connection != null && svr.Connection.IsConnected) { svr.Connection.Dispose(); } } }
/// <summary> /// GotoActivity /// </summary> /// <param name="procInstID"></param> /// <param name="activityName"></param> public static void GotoActivity(int procInstID, string activityName) { Mang.WorkflowManagementServer svr = new Mang.WorkflowManagementServer(); try { svr.CreateConnection(); svr.Connection.Open(GetServerConnectionSetup()); svr.GotoActivity(procInstID, activityName); } catch { throw; } finally { if (svr != null) { svr.Connection.Close(); } } }
/// <summary> /// Method to prune collection by removing errors for process instances that are not in error state anymore. /// </summary> /// <param name="client">K2 Client connection for retreiving specific process instances.</param> public void Prune(k2Client.Connection client, k2Mgnt.WorkflowManagementServer server) { int i = 0; while (i < Count) { bool found = false; bool remove = false; k2Client.ProcessInstance procInst = null; // check if the process instance still exists and is not deleted if (ProcessInstanceExists(this[i].ProcInstId, server)) { // get the process instance that's logged procInst = client.OpenProcessInstance(this[i].ProcInstId); // process instance is still alive! found = true; // now determine whether or not the process instance is still in an error state. // if it is no longer in an error state, flag that it can be removed from the error collection. if (procInst.Status1 != k2Client.ProcessInstance.Status.Error) { remove = true; } } // if the associated process instance is no longer in an error state OR it has since been // 'fixed and completed' or simply deleted, remove the error item from the error collection. if ((!found) || remove) { RemoveAt(i); _isDirty = true; } else { i++; } } }
/// <summary> /// Method to check K2 for process errors since the last scheduled internal. /// </summary> public void RetrieveNewErrors() { k2Mgnt.WorkflowManagementServer server = new k2Mgnt.WorkflowManagementServer(); server.Connection = server.CreateConnection(); server.Connection.Open(connectionString); // client connection to K2 server. k2Client.Connection k2Conn = new k2Client.Connection(); try { // Search for new process errors in the specified profile and update the error list. k2Mgnt.ErrorLogs els = server.GetErrorLogs(server.GetErrorProfile(_errorProfileName).ID); // Open client connection to K2 server. string host = Regex.Replace(connectionString, @"(.+)(Host\=)(.+)(;)(.+)", "$3"); k2Conn.Open(host); // check every error in the error log foreach (k2Mgnt.ErrorLog el in els) { Update(el, k2Conn); } if (ErrorsToBeReported.Count > 0) { // // Pull in the related tasks into the error collection. // k2Mgnt.WorklistItems worklistItems = server.GetWorklistItems( new DateTime(1900, 1, 1), DateTime.Now, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty); foreach (SourceCode.Workflow.Management.WorklistItem worklistItem in worklistItems) { if (worklistItem.ProcessInstanceStatus == k2Mgnt.ProcessInstanceStatus.Error) { Update(worklistItem); } } } //Finally, prune the errors collection. Prune(k2Conn, server); } finally { if (server.Connection != null) { server.Connection.Close(); } if (k2Conn != null) { k2Conn.Close(); } } }
/// <summary> /// 连接K2管理服务器 /// </summary> /// <returns></returns> protected WorkflowManagementServer ConnectManagermentServer() { try { var wms = new SourceCode.Workflow.Management.WorkflowManagementServer(); wms.CreateConnection(); wms.Open(K2ManagermentConnStr); return wms; } catch (Exception ex) { throw new K2ManagermentServerConnectErrorException(K2ManagermentConnStr, ex); } }