Introduction
Developers can easily make use of task management features offered by Aspose.Workflow. In this topic, we will discuss the task management based on a process instance or a user. To manage something, we need to have that thing first. So, before managing workflow tasks, we need to retrieve these tasks first. There are two ways to retrieve workflow tasks as follows:
- Tasks associated with a process instance
- Tasks associated with a user
Tasks Based on a Process Instance
Developers can obtain a list of all workflow tasks associated to any process instance by calling the GetAllTasks method of IProcessInstance interface. GetAllTasks method takes a boolean parameter, true or false. If you want only pending tasks, just pass true otherwise false to retreive all tasks as shown below in the example.
Example:
[C#]
//Instantiate an object of EngineManager
EngineManager manager = EngineManager.Instance;
//Configure the EngineManager object
manager.Configure();
//Create an object of IEngine interface
IEngine engine = manager.Engine;
//Login to the Aspose.Workflow engine
engine.Login("aspose","aspose");
//Create an instance of IInstanceAdmin interface
IInstanceAdmin instanceAdmin = engine.InstanceAdmin;
//Access a specific process instance by passing its instance ID
IProcessInstance instance = instanceAdmin.GetProcessInstance(
"1_TestInstance_TestInstance_Wor1");
//Retrieving all tasks related to the specified process instance
ITask[] tasks=instance.GetAllTasks(false);
[VB.NET]
'Instantiate an object of EngineManager
Dim manager As EngineManager = EngineManager.Instance
'Configure the EngineManager object
manager.Configure()
'Create an object of IEngine interface
Dim engine As IEngine = manager.Engine
'Login to the Aspose.Workflow engine
engine.Login("aspose","aspose")
'Create an instance of IInstanceAdmin interface
Dim instanceAdmin As IInstanceAdmin = engine.InstanceAdmin
'Access a specific process instance by passing its instance ID
Dim instance As IProcessInstance = instanceAdmin.GetProcessInstance(
"1_TestInstance_TestInstance_Wor1")
'Retrieving all tasks related to the specified process instance
Dim tasks() As ITask = instance.GetAllTasks(False)
Tasks Based on a User
For obtaining all tasks associated to some user, developers would need to call GetAllTasks method of IEngine.Worklist object but before calling this method, it would be needed to connect to Aspose.Workflow engine using the Username and Password of the desired user. For further details, refer to Access all tasks related to a user .
Example:
[C#]
//Login to Aspose.Workflow engine for user "jack"
engine.Login("jack","baldwin");
//Retrieving all tasks associated to "jack"
ITask[] tasks=engine.Worklist.GetAllTasks();
[VB.NET]
'Login to Aspose.Workflow engine for user "jack"
engine.Login("jack","baldwin")
'Retrieving all tasks associated to "jack"
Dim tasks() As ITask = engine.Worklist.GetAllTasks()
Task Management
All kinds of workflow tasks (either associated to a process instance or a user) are managed in the same way using IEngine.Worklist object, which provides us an instance of IWorklist2 interface. IWorklist2 interface is extended from IWorklist interface that has many methods to manage tasks in a variety of ways as follows:
- Accepting Tasks
- Releasing Tasks
- Completing Tasks
- Re-assigning Tasks
Accepting Tasks
Whenever a user wants to work on some workflow task, he needs to accept that task first by calling AcceptTask method of IEngine.Worklist object. After a task is accepted by some user, it is assigned to that one and becomes invisible to other users disallowing them to work on that task.
Example:
[C#]
//Accepting a workflow task
engine.Worklist.AcceptTask(task);
[VB.NET]
'Accepting a workflow task
engine.Worklist.AcceptTask(task)
Before completing, releasing or reassigning a task, it's better to check that whether that task has already been accepted or not. It can be done by calling IsTaskAccepted method of IEngine.Worklist object, which takes the task ID and returns a boolean value to indicate that the specified task is already accepted or not.
Example:
[C#]
//Accepting a workflow task if it is not already accepted
if(!engine.Worklist.IsTaskAccepted(task.Id))
{
engine.Worklist.AcceptTask(task);
}
[VB.NET]
'Accepting a workflow task if it is not already accepted
If Not engine.Worklist.IsTaskAccepted(task.Id) Then
engine.Worklist.AcceptTask(task)
End If
Releasing Tasks
For releasing workflow tasks due to any reason, developers can make use of ReleaseTask method of IEngine.Worklist object as shown below in the code snippet.
Example:
[C#]
//Accepting a workflow task if it is not already accepted
if(!engine.Worklist.IsTaskAccepted(task.Id))
{
engine.Worklist.AcceptTask(task);
}
//Releasing a workflow task
engine.Worklist.ReleaseTask(task);
[VB.NET]
'Accepting a workflow task if it is not already accepted
If Not engine.Worklist.IsTaskAccepted(task.Id) Then
engine.Worklist.AcceptTask(task)
End If
'Releasing a workflow task
engine.Worklist.ReleaseTask(task)
Completing Tasks
After a user finishes his work required for some workflow task, user can call CompleteTask method of IEngine.Worklist object to complete that task as shown below in the code snippet. CompleteTask method also allows users to pass the instance variables for the task in the form of an IDictionary object. These variables are used for task completion. But, if you don't want to pass these instance variables then simply pass null instead of IDictionary object.
Completing Task Without Setting Variables
Example:
[C#]
//Accepting a workflow task if it is not already accepted
if(!engine.Worklist.IsTaskAccepted(task.Id))
{
engine.Worklist.AcceptTask(task);
}
//Complete task but don't set the instance variables
engine.Worklist.CompleteTask(task,null);
[VB.NET]
'Accepting a workflow task if it is not already accepted
If Not engine.Worklist.IsTaskAccepted(task.Id) Then
engine.Worklist.AcceptTask(task)
End If
'Complete task but don't set the instance variables
engine.Worklist.CompleteTask(task,Nothing)
Completing Task With Setting Variables
Example:
[C#]
//Accepting a workflow task if it is not already accepted
if(!engine.Worklist.IsTaskAccepted(task.Id))
{
engine.Worklist.AcceptTask(task);
}
//Set variables
IDictionary vars = new Hashtable();
vars["Var1"] = "Value1";
vars["Var2"] = "Value2";
//Complete task and pass variables
engine.Worklist.CompleteTask(task,vars);
[VB.NET]
'Accepting a workflow task if it is not already accepted
If Not engine.Worklist.IsTaskAccepted(task.Id) Then
engine.Worklist.AcceptTask(task)
End If
'Set variables
Dim vars As IDictionary = New Hashtable()
vars("Var1") = "Value1"
vars("Var2") = "Value2"
'Complete task and pass variables
engine.Worklist.CompleteTask(task,vars)
Re-assigning Tasks
If a user wants some other person to work on a workflow task then ReassignTask method of IEngine.Worklist object will be called by the user as shown below in the code snippet.
Example:
[C#]
//Accepting a workflow task if it is not already accepted
if(!engine.Worklist.IsTaskAccepted(task.Id))
{
engine.Worklist.AcceptTask(task);
}
//Reassign the task to "james"
engine.Worklist.ReassignTask(task, "james");
[VB.NET]
'Accepting a workflow task if it is not already accepted
If Not engine.Worklist.IsTaskAccepted(task.Id) Then
engine.Worklist.AcceptTask(task)
End If
'Reassign the task to "james"
engine.Worklist.ReassignTask(task, "james")