public ResultModel ApproveK2Process(string sn, int loginId, string actionString, string memo, Dictionary <string, string> dataFields, out string activityName, out string processCode, out int procInstID) { Connection k2Connect = null; WorklistItem workList = null; ResultModel jr = new ResultModel() { Code = ResultCode.Fail }; activityName = string.Empty; processCode = string.Empty; procInstID = 0; try { k2Connect = new Connection(); k2Connect.Open(ConfigurationManager.AppSettings["K2Server"], ConfigurationManager.AppSettings["K2LoginString"]); k2Connect.ImpersonateUser(SecurityLable(loginId.ToString())); workList = k2Connect.OpenWorklistItem(sn); if (workList != null) { #region 更新Datafield if (dataFields != null && dataFields.Count > 0) { ProcessInstance CurrentProcessInst = workList.ProcessInstance; //更新Datafields foreach (string key in dataFields.Keys) { if (CurrentProcessInst.DataFields[key] != null) { if (CurrentProcessInst.DataFields[key].Value.ToString() != dataFields[key]) { CurrentProcessInst.DataFields[key].Value = dataFields[key]; } } } } #endregion #region 审批任务 //批量审批没有actionString,默认第一个操作 if (string.IsNullOrEmpty(actionString)) { if (workList.Actions[0].Name == REJECTACTION) { workList.GotoActivity("流程未通过"); } else if (workList.Actions[0].Name == UNDOACTION) { workList.GotoActivity("流程撤销"); } else { workList.Actions[0].Execute(); } } else { //执行匹配的操作 if (actionString == UNDOACTION) { workList.GotoActivity("流程撤销"); } else if (actionString == REJECTACTION) { workList.GotoActivity("流程未通过"); } else { bool isExcute = false; for (int i = 0; i < workList.Actions.Count; i++) { if (workList.Actions[i].Name == actionString) { workList.Actions[i].Execute(); isExcute = true; break; } } if (!isExcute) { jr.Code = ResultCode.Fail; jr.Msg = string.Format("Action:{0}错误", actionString); return(jr); } } } #endregion jr.Code = ResultCode.Sucess; jr.Msg = ""; activityName = workList.ActivityInstanceDestination.Name; processCode = workList.ProcessInstance.DataFields["ProcessCode"].Value.ToString(); procInstID = workList.ProcessInstance.ID; } } catch (Exception ex) { jr.Msg = ex.Message; } finally { if (workList != null) { if (workList.Status == WorklistStatus.Open) { try { k2Connect.RevertUser(); workList.Release(); } catch { } } } if (k2Connect != null) { k2Connect.Close(); } } return(jr); }
/// <summary> /// 审批流程 /// </summary> /// <param name="userName"></param> /// <param name="sn"></param> /// <param name="actionString"></param> /// <param name="memo"></param> /// <param name="dataFields"></param> public WorklistItem ApprovalProcess(string userName, string sn, string actionString, string memo, Dictionary <string, string> dataFields) { Connection k2Connection = new Connection(); try { k2Connection.Open(ConfigurationBase.Web.K2Server, ConfigurationBase.Web.K2LoginString); k2Connection.ImpersonateUser(userName); //根据SN打开工作项 WorklistItem workList = k2Connection.OpenWorklistItem(sn); if (workList != null) { #region 更新Datafield if (dataFields != null && dataFields.Count > 0) { ProcessInstance currentProcessInst = k2Connection.OpenProcessInstance(workList.ProcessInstance.ID); //更新Datafields foreach (string key in dataFields.Keys) { if (currentProcessInst.DataFields[key] != null) { if (currentProcessInst.DataFields[key].Value != dataFields[key]) { currentProcessInst.DataFields[key].Value = dataFields[key]; } } } currentProcessInst.Update(); } #endregion #region 审批任务 //批量审批没有actionString,默认第一个操作 if (string.IsNullOrEmpty(actionString)) { if (workList.Actions[0].Name == REJECTACTION) { workList.GotoActivity("流程未通过"); } else if (workList.Actions[0].Name == UNDOACTION) { workList.GotoActivity("流程撤销"); } else { workList.Actions[0].Execute(); } } else { //执行匹配的操作 if (actionString == UNDOACTION) { workList.GotoActivity("流程撤销"); } else if (actionString == REJECTACTION) { workList.GotoActivity("流程未通过"); } else { bool isExcuted = false; for (int i = 0; i < workList.Actions.Count; i++) { if (workList.Actions[i].Name == actionString) { workList.Actions[i].Execute(); isExcuted = true; break; } } } } #endregion } return(workList); } catch (Exception) { return(null); } finally { if (k2Connection != null) { k2Connection.Close(); } } }