Delegate or Un-delegate a Worklistitem

Delegation will permit someone else, in addition to the original task owner, to action a task. Below is a code sample illustrating how to programmatically implement a Delegation.

It is not possible to “delete” the original destination user from a worklist item if that worklist item was delegated, because the delegated item is shared between the original user and the delegated user. To “delete” the original destination user, redirect the worklist item instead. Attempting to delete the original destination user may result in an error like “The statement terminated. The maximum recursion 100 has been exhausted before statement completion”

This sample code requires references to the assemblies:

  • SourceCode.HostClientAPI
  • SourceCode.Workflow.Management

Copy

Implementing a Delegation

//TODO: build a connection string with the SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder
SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder connectionString = new SCConnectionStringBuilder();
connectionString.Host = "[k2servername]";
//alternatively build a simple connection
WorkflowManagementServer workflowServer = new WorkflowManagementServer("localhost", 5555);

try {
    //declare variables
    int nProcInstID = 1; //process instance ID
    int nActID = 2; //activity ID for the activity containing the task to delegate
    int nActInstID = 3; //activity instance ID for the task to delegate
    int nActDestID = 4; //activity instance destination ID for the task to delegate
    int nEventID = 5; //event ID for the client event to delegate
    string strUserName = @"K2:[Domain]\[Username]"; // must include the appropriate security label ("K2:" or "K2SQL:")
    bool bAddUser = true; // TRUE = add the user rights for this task; FALSE = delete the user rights this task

    workflowServer.Open();

    //create an Actioner object for the user to delegate tasks to
    Actioner oActioner = new Actioner();
    oActioner.ActionerType = ActionerType.User;
    oActioner.Name = strUserName;

    // locate the currently assigned rights for this worklist item
    ActionInstanceRights arRights = workflowServer.GetActionInstanceRights(nProcInstID, nActID, nActInstID, nActDestID, nEventID);

    // check to see if this user already exists for this task
    ActionInstanceRight oExistingRight = null;
    foreach(ActionInstanceRight oRight in arRights) {
        if (oRight.Actioner.Name.ToUpper() == strUserName.ToUpper()) {
            oExistingRight = oRight;
            break;
        }
    }

    // now attempt to add or delete user rights for this task
    if (bAddUser) {
        // create a new user for this task if one does not already exist
        if (oExistingRight == null) {
            ActionInstanceRight oActInstRight = new ActionInstanceRight();
            oActInstRight.ProcInstID = nProcInstID;
            oActInstRight.ActID = nActID;
            oActInstRight.ActInstID = nActInstID;
            oActInstRight.ActInstDestID = nActDestID;
            oActInstRight.EventID = nEventID;
            oActInstRight.Actioner = oActioner;
            workflowServer.CreateActionInstanceRight(oActInstRight);
        }
    } else {
        // delete a user for this task if it already exists
        if (oExistingRight != null) {
            ActionInstanceRights arTmpActInstRights = new ActionInstanceRights();
            arTmpActInstRights.Add(oExistingRight);
            workflowServer.DeleteActionInstanceRights(arTmpActInstRights);
        }
    }
} catch(Exception ex) {
    //do something with the error
} finally {
    //always close the connection
    workflowServer.Connection.Close();
}