Thursday, March 1, 2012

How to: Create State Machine Workflow in Sharepoint 2010

Requirement: Create workflow send document from admin to client => client will check validation, if document is not valid, system automatic feedback to admin then admin edit and send to client ... When client check and comment is Approved => workflow completed.

Download video at here and project at here
Open Visual studio 2010 | New Project | Expand Visual C# | Sharepoint | 2010 | choose State Machine Workflow template and name is StateMachineWorkflow
Choose “Deploy as a farm solution”
Set default and click Next
Choose follows as and click Next
Set default and click Finish
User Interface (UI) follows as:
Drag and drop 3 state control from Toolbox | Windows Workflow v3.0 to panel State Machine Workflow
Right click Workflow1InitialState and set Name is InitialState
Right click stateActivity1 and set Name is stateInProcess
Right click stateActivity2 and set Name is statePreview
Right click stateActivity3 and set Name is stateFinished
Next, Right click InitialState | choose “Set as Initial State”
Next, Right click stateFinished| choose “Set as Completed State”
Next, Double click to eventDrivenActivity1 in state InitialState
Next, UI follows as then drag and drop SetState control in Toolbox to panel State Machine Workflow follows as:
Right click setStateActivity1 and set TargetStateName is stateInProcess from dropdownlist
Click to Workflow1 as image follows:
Next, UI follows as and right click to stateInProcess then choose AddStateInitialzation
Next, UI follows as:
Next, UI follows as then drag and drop CreateTask control in Toolbox to here follows as:
Next, Right click to createTask1 | Properties | In CorrelationToken enter InProcessToken then Expand it, at OwnerActivityName choose stateInProcess from dropdownlist
Next, set TaskId by click button … follows as
Click tab Bin to a new member | Create Field | OK
Next, set TaskProperties by click button … follows as 
Click tab Bin to a new member | Create Field | OK
Next, Double click to createTask1 in stateInProcess
Next, copy code and paste to method generated follows as:
Code here

createTask1_TaskId1 = Guid.NewGuid();
createTask1_TaskProperties1.Title = "Finish Document";
createTask1_TaskProperties1.AssignedTo = @"quochung-axioo\admin";
createTask1_TaskProperties1.DueDate = DateTime.Now.AddDays(1.0);

Click to tab Workflow1.cs[Design] then click Workflow1 follows as:
Next, right click to stateInProcess | choose “Add EventDrive”
Next, UI follows as:
Next, UI follows as then drag and drop OnTaskChanged control in Toolbox panel State Machine Workflow follows as:
Next, Right click to onTaskChanged1 | Properties | set TaskId by click button … follows as
Next, in tab “Bind to an existing member” | choose createTask1_TaskId1 | OK
Next, set AfterProperties  by click button … follows as
Click tab Bin to a new member | Create Field | OK
Next, set BeforeProperties  by click button … follows as
Click tab Bin to a new member | Create Field | OK
Next, set CorrelationToken is InProcessToken from Dropdownlist then expand it and set OwnerActivityName is stateInProcess
Next, Double click to onTaskChanged1
Copy code and paste to method follows as:
Code here
onTaskChanged1_AfterProperties1 = onTaskChanged1.AfterProperties;
onTaskChanged1_BeforeProperties1 = onTaskChanged1.BeforeProperties;

Next, drag and drop IfElse control in Toolbox  to below onTaskChanged1 follows as:
Click to tab Workflow1.cs then copy code and paste here follows as:
Code here
private void ReadyForReview(object sender, ConditionalEventArgs e)
        {
            if (onTaskChanged1_AfterProperties1.PercentComplete == 1.0)
            {
                e.Result = true;
            }
            else
            {
                e.Result = false;
            }
        }
Right click to ifElseBranchActivity1 and set Condition is Code Condition then expand Condition and at sub Condition choose method ReadyForReview from dropdownlist
Next, drag and drop setState control in Toolbox to here follows as:
Right click setStateActivity2 and set TargetStateName is statePreview from dropdownlist
Next, Click to Workflow1 follows as:
Next, UI follows as:
Next, right click to statePreview | choose “AddStateInitialization”
Next, UI follows as:
Next, drag and drop CreateTask control in Toolbox to here follows as:
Next, Right click to createTask2 | Properties | In CorrelationToken enter ReviewStateToken then Expand it, at OwnerActivityName choose stateReview from dropdownlist
Next, set TaskId by click button … follows as
Next, Click tab Bin to a new member | Create Field | OK
Next, set TaskProperties by click button … follows as
Click tab Bin to a new member | Create Field | OK
Next, double click to createReviewTask
Next, copy code to this method
Code here
createReviewTask_TaskId1 = Guid.NewGuid();
createReviewTask_TaskProperties1.Title = "Review Document";
createReviewTask_TaskProperties1.AssignedTo = @"quochung-axioo\client";
createReviewTask_TaskProperties1.DueDate = DateTime.Now.AddDays(1.0);

Click to tab Workflow1.cs[Design] then click Workflow1 follows as:
Next, right click to stateReview | choose “Add EventDrive”
Next, UI follows as:
Next, drag and drop OnTaskChanged control in Toolbox to here follows as:
Next, Right click to OnTaskChanged2 | Properties |, set CorrelationToken is ReviewStateToken from Dropdownlist then expand it and set OwnerActivityName is stateReview

Next, set TaskId by click button … follows as
Next, in tab “Bind to an existing member” | choose createReviewTask1_TaskId1 | OK
Next, set AfterProperties  by click button … follows as
Click tab “Bin to a new member” | Create Field | OK
Next, set BeforeProperties  by click button … follows as
Click tab “Bin to a new member” | Create Field | OK
Next, Double click to onTaskChanged2
Copy code and paste to method follows as:
Code here
onTaskChanged2_AfterProperties1 = onTaskChanged2.AfterProperties;
onTaskChanged2_BeforeProperties1 = onTaskChanged2.BeforeProperties;
Next, drag and drop IfElse control in Toolbox to below onTaskChanged2 follows as:
Next, click tab Workflow1.cs (code) and paste code to here
Code here

private void ReviewFinished(object sender, ConditionalEventArgs e)
        {
            if (onTaskChanged2_AfterProperties1.PercentComplete == 1.0)
            {
                e.Result = true;
            }
            else
            {
                e.Result = false;
            }
        }
Right click to ifElseBranchActivity3 and set Condition is Code Condition then expand Condition and at sub Condition choose method ReviewFinished from dropdownlist
Next, drag and drop IfElse control in Toolbox  to below ifElseBranchActivity3 follows as:
Next, click tab Workflow1.cs (code) and paste code to here
private void DocApproved(object sender, ConditionalEventArgs e)
        {
            if (onTaskChanged2_AfterProperties1.Description == "<DIV>Approved</DIV>")
            {
                e.Result = true;
            }
            else
            {
                e.Result = false;
            }
        }

Right click to ifElseBranchActivity5 and set Condition is Code Condition then expand Condition and at sub Condition choose method DocApproved from dropdownlist
Next, UI as follows then drag and drop SetState control in Toolbox to below ifElseBranchActivity5 follows as:
Right click setStateActivity3 and set TargetStateName is stateFinished from dropdownlist
Next, UI as follows then drag and drop SetState control in Toolbox to below ifElseBranchActivity6 follows as:
Right click setStateActivity3 and set TargetStateName is stateInProgress from dropdownlist
Build and deploy.
Open your site | Click Shared Documents and add any document
Next, you see status of workflow is In Process
Click to Task List you see new task assigned for admin then you edit item
Next, enter % Complete is 100
Next, after check document and save task assigned for Client then you edit item
Next, enter % Complete is 100
Next, after check document and save task assigned for admin then you edit item
Next, enter % Complete is 100
Next, after check document and save task assigned for client then you edit item
Next, enter % Complete is 100, special when comment is “Approved” in Description is workflow completed
Task is finished
Now, you see document is approved

Read it:

The State Machine Workflow project in Visual Studio 2010 provides a graphical design surface in which a workflow can be built.
·         The Initial State has one activity of SetState that takes the workflow directly to the stateInProgress state.
·         The stateInProgress state has a task activity that generates a new task titled "Finish Document" and assigns the task to a specified user.
·         The stateInProgress state has an onTaskChanged activity that is invoked when the task changes. TheinTaskChanged activity has an IfElse statement that compares the "percent complete" of the task to 1.0 (100%).
·         If the condition is true, the workflow transitions to the next state (stateReview). If it is false, the workflow remains at this state.
·         In the stateReview state, a task is generated with a title of "Review Document" as part of the state initialization, and again this task is assigned to a specified user.
·         The stateReview state is invoked when the task changes, and an IfElse statement compares the "percent complete" of this task with 100 percent. As before, when the task is 100 percent complete, the workflow continues.
·         When the task is 100 percent complete, a nested IfElse statement is reached within the stateReviewstate. The code compares the text that appears in the task description to the string "<DIV>Approved</DIV>".
·         If the string matches, the stage is considered complete and the workflow continues to StateFinished. If the string does not match, the state returns to stateInProgress.

4 comments:

  1. Great Blog exactly what i wanted !!!Dude u rock~!!!!!!! :))))

    ReplyDelete
  2. I am looking for and I love to post a comment that "The content of your post is awesome" Great work! imprest amount

    ReplyDelete
  3. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. and Subscribers Views Buy YouTube

    ReplyDelete