Setting permissions on a workflow

The following example shows how to assign Administer and Start permissions on a specific workflow for a particular user.

This sample code requires references to the assemblies:

  • SourceCode.HostClientAPI
  • SourceCode.Workflow.Management

Copy

Assign Administer and Start permissions

//TODO: build a connection string with the SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder
SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder connectionString = new SCConnectionStringBuilder();
connectionString.Host = "[k2servername]";

WorkflowManagementServer workflowServer = new WorkflowManagementServer();

try {
    workflowServer.CreateConnection();
    workflowServer.Connection.Open(connectionString.ToString());

    // Create instances of the Permissions and ProcSetPermissions class
    SourceCode.Workflow.Management.Permissions permissions = new Permissions();
    SourceCode.Workflow.Management.ProcSetPermissions procPermissions = new ProcSetPermissions();

    //the user we want to assign permissions for
    string userName = @"[label}:[domain]\[username]";

    //Set up the process permissions for a specific process definition (ProcSetId)
    procPermissions.ProcSetID = 1;
    procPermissions.UserName = userName;
    //set permissions
    procPermissions.Admin = true;
    procPermissions.Start = true;
    procPermissions.View = false;
    procPermissions.ViewPart = false;
    procPermissions.ServerEvent = false;

    // Add the ProcSetPermissions object to the Permissions object
    // you can add multiple ProcSetPermissions to the same Permissions object, if needed
    permissions.Add(procPermissions);

    // Update the permissions for a user
    bool updated = workflowServer.UpdateUserPermissions(userName, permissions);
    // or, update the permissions for a process definition
    bool updated1 = workflowServer.UpdateProcPermissions(1, permissions);
    // or, update or add permissions
    bool updated2 = workflowServer.UpdateOrAddProcUserPermissions(1, permissions);
} catch(Exception ex) {
    //do something with the error
} finally {
    //always close the connection
    workflowServer.Connection.Close();
}