Introduction
This tip shows how to create custom state machine workflow using Visual Studio with the step by step snippets of it. It is useful because it covers the basics of state machine workflow and how it configures. This tip will also help to configure the properties of each activity and its methods.
When workflow starts, it assigns the task to the originator of workflow. When workflow task percentage completes set to 100% it goes to the next state (second task). You can see the second task in the task list. In the second task, if you are not setting the percentage complete 100% the workflow goes back to the first state (first task), otherwise it is in finalize state.
Follow these Steps
- Create new state machine workflow project or Add state machine workflow item into existing project.
- It automatically creates
WorkflowInitialstate
state in it.
- In this event driven activity
onworkflowactivated
is automatically created.
Put setStateActivity
in it for the purpose of calling another state.
Add state from tools on design and bind it to setstateActivity
property TargetStateName
of InitialState
. After setting the TargetStateName
, you can see the link between them.
- First Task:
Set
stateInitializationActivity
, eventdrivenactivity
to the state (First Task).
In stateInitializationActivity of first task, set the createtask activity in it.
Set the properties of createtask
:
- Create its new
correlationToken(FirstTaskToken)
and set its owneractivityname
as its state (First Task) - Create its
TaskId
(Createtask1_TaskId
)and TaskProperties
.
In eventdrivenactivity, set ontaskchanged activity in it.
- Set
ontaskchanged
properties
- Assign
correlationToken (FirstTaskToken)
- Assign
TaskId (createtask1_TaskId)
- Create its after properties.
- Drag
Ifelse
activity on design.
setStateActivity
on left part (if). If required set logtohistorylistactivity
- set properties of
setStateActivity targetStateName
as another state(SecondTask
).
- set
logtohistorylistactivity
property HistoryDescription
for record history.
- Set the code condition of
ifElseBranchActivity
.
- Second Task:
Perform the same activity as the first task. And set its
setStateActivity targetStateName
as FinalTask
.
Here, the only change is that set extra setStateActivity
to else
part (right side) for state change to any other state (Here workflow switches to firsttask
).
- Final Task:
- Drag one state to design.
- Set it as final activity by choosing the "Set as complete state". See the below image.
- Finally workflow looks like:
The code is as follows:
public sealed partial class StateMachineWorkflow : StateMachineWorkflowActivity
{
public StateMachineWorkflow()
{
InitializeComponent();
}
public SPWorkflowActivationProperties workflowProperties =
new SPWorkflowActivationProperties();
public Guid createTask1_TaskId = default(System.Guid);
public SPWorkflowTaskProperties createTask1_TaskProperties =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
public SPWorkflowTaskProperties onTaskChanged1_AfterProperties =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
private void createTask1_MethodInvoking(object sender, EventArgs e)
{
createTask1_TaskId = Guid.NewGuid();
createTask1_TaskProperties.Title = "First Task";
createTask1_TaskProperties.PercentComplete = 0f;
createTask1_TaskProperties.Description = "This is the first task
created after its percentage 100% done workflow goes to second state.";
createTask1_TaskProperties.AssignedTo = workflowProperties.OriginatorUser.LoginName;
}
private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)
{
onTaskChanged1_AfterProperties = onTaskChanged1.AfterProperties;
}
private void FirstTaskCondition(object sender, ConditionalEventArgs e)
{
if (onTaskChanged1_AfterProperties.PercentComplete == 1.0)
{
e.Result = true;
}
else
{
e.Result = false;
}
}
public Guid createTask2_TaskId = default(System.Guid);
public SPWorkflowTaskProperties createTask2_TaskProperties =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
public SPWorkflowTaskProperties onTaskChanged2_AfterProperties =
new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
private void createTask2_MethodInvoking(object sender, EventArgs e)
{
createTask2_TaskId = Guid.NewGuid();
createTask2_TaskProperties.Title = "Second Task";
createTask2_TaskProperties.PercentComplete = 0f;
createTask2_TaskProperties.Description = "This is the second task
created after its percentage 100% done workflow goes to
final state otherwise back to first task state.";
createTask2_TaskProperties.AssignedTo = workflowProperties.OriginatorUser.LoginName;
}
private void onTaskChanged2_Invoked(object sender, ExternalDataEventArgs e)
{
onTaskChanged2_AfterProperties = onTaskChanged2.AfterProperties;
}
private void SecondTaskCondition(object sender, ConditionalEventArgs e)
{
if (onTaskChanged2_AfterProperties.PercentComplete == 1.0)
{
e.Result = true;
}
else
{
e.Result = false;
}
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.