//sample that shows how to perform Live INstance Management //in this example, we want to migrate all active instances of a workflow to the latest version //Note: take due care when migrating active process instances since not all migration scenarios are supported public void LiveInstanceManagementSample() { //establish the connection WorkflowManagementServer K2Mgmt = new WorkflowManagementServer(); K2Mgmt.CreateConnection(); K2Mgmt.Connection.Open("connectionstring"); //get the proc set ID of the workflow so that we can get the latest version of the proc set SourceCode.Workflow.Management.Criteria.ProcessCriteriaFilter filter = new SourceCode.Workflow.Management.Criteria.ProcessCriteriaFilter(); Processes procs = null; filter.AddRegularFilter(ProcessFields.ProcessFullName, Comparison.Equals, "processFullName"); procs = K2Mgmt.GetProcesses(filter); int procSetId = procs[0].ProcSetID; //now get the latest version of the procset int latestVersion = 0; int latestVersionId = 0; Processes procVersions = K2Mgmt.GetProcessVersions(procSetId); foreach (Process procVersion in procVersions) { if (procVersion.VersionNumber > latestVersion) { latestVersion = procVersion.VersionNumber; latestVersionId = procVersion.ProcID; } } //now that we have the latest version of the workflow, //get the ID's of all the process instances of the workflow ProcessInstances procInstances = K2Mgmt.GetProcessInstancesAll("processFullName", "", ""); //leaving parameters blank effectively ignores that parameter foreach (ProcessInstance procInst in procInstances) { //no need to migrate ones that are already on this version if (procInst.ExecutingProcID != latestVersionId) { //must stop a non-stopped instance before attempting migration if (procInst.Status != "Stopped") { K2Mgmt.StopProcessInstances(procInst.ID); } //now migrate the instance to the latest version K2Mgmt.SetProcessInstanceVersion(procInst.ID, latestVersion); //restart the process K2Mgmt.StartProcessInstances(procInst.ID); } } //close the connection K2Mgmt.Connection.Close(); }
public static void SetProcessInstanceVersion() { WorkflowManagementServer wms = new WorkflowManagementServer(); wms.Open(GetK2ManagementServerConnectionString()); // wms.StopProcessInstances SqlHelper sqlHelper = new SqlHelper(K2ConnectionString); var dt = sqlHelper.ExecuteDataTable("SELECT ID FROM Server.ProcInst WHERE ProcID IN (1202,1217,1195,1176)"); if (dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { wms.StopProcessInstances(Convert.ToInt32(dr["ID"])); wms.SetProcessInstanceVersion(Convert.ToInt32(dr["ID"]), 68); wms.StartProcessInstances(Convert.ToInt32(dr["ID"])); } } }
new string[] { "ProcessInstanceId", "NewVersion", "ActivityName", "ResultStatus", "ResultMessage" })] // , "IsDefaultVersion", "VersionDate", "VersionDescription", "VersionLabel", "VersionNumber" public LIM.ProcessInstance MigrateProcessInstance() { List<LIM.ProcessInstance> results = new List<ProcessInstance>(); WorkflowManagementServer svr = new WorkflowManagementServer("localhost", 5555); try { svr.Open(); svr.StopProcessInstances(this.ProcessInstanceId); svr.SetProcessInstanceVersion(this.ProcessInstanceId, this.NewVersion); if (!string.IsNullOrWhiteSpace(this.ActivityName)) { svr.GotoActivity(this.ProcessInstanceId, ActivityName); } else { svr.StartProcessInstances(this.ProcessInstanceId); } } catch (Exception ex) { this.ResultStatus = "Exception"; this.ResultMessage = ex.GetBaseException().Message; return this; } finally { try { svr.Connection.Close(); svr.Connection.Dispose(); } catch { } svr = null; } this.ResultStatus = "Success"; return this; }