//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();
        }
        //sample that shows how to set workflow permissions
        //in this sample, we are giving a dummy user account rights to a dummy process
        public void SetWorkflowPermissions()
        {
            //establish the connection
            WorkflowManagementServer K2Mgmt = new WorkflowManagementServer();

            K2Mgmt.CreateConnection();
            K2Mgmt.Connection.Open("connectionstring");

            //get the proc set ID of the workflow whose permissions we need to 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 that we have the proc set ID, we can apply permissions
            //build up a collection of permissions to apply to the workflow
            Permissions permissions = new Permissions();
            //create a ProcSetPermission object - this object will be added to the Permissions collection
            ProcSetPermissions procPermissions = new ProcSetPermissions();

            procPermissions.ProcSetID   = procSetId;
            procPermissions.UserName    = @"domain\username"; //could also be ProcPermissions.GroupName for a group
            procPermissions.Admin       = true;
            procPermissions.Start       = true;
            procPermissions.View        = true;
            procPermissions.ViewPart    = false;
            procPermissions.ServerEvent = false;
            //add the permission to the permissions collection
            permissions.Add(procPermissions);
            procPermissions = null;

            //now we can apply the updated set of permissions to the process
            K2Mgmt.UpdateProcPermissions(procSetId, permissions);

            //several other methods are also available, listed below. Check product documentation for more information
            //K2Mgmt.UpdateGroupPermissions();
            //K2Mgmt.UpdateOrAddProcUserPermissions();
            //K2Mgmt.UpdateProcGroupPermissions();
            //K2Mgmt.UpdateProcUserPermissions();

            //close the connection
            K2Mgmt.Connection.Close();
        }