Download source at: http://www.mediafire.com/?2t18uz84x7f7btk
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.
Open Visual studio 2010
| New Project | Expand Visual C# | Sharepoint | 2010 | choose State Machine
Workflow template and name is StateMachineWorkflow
Next, UI follows as then
drag and drop SetState control in Toolbox to panel State Machine Workflow
follows as:
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, UI follows as then
drag and drop OnTaskChanged control in Toolbox panel State Machine Workflow
follows as:
Next, set
CorrelationToken is InProcessToken from Dropdownlist then expand it and set
OwnerActivityName is stateInProcess
onTaskChanged1_AfterProperties1
= onTaskChanged1.AfterProperties;
onTaskChanged1_BeforeProperties1
= onTaskChanged1.BeforeProperties;
Next, drag and drop
IfElse control in Toolbox to below onTaskChanged1 follows as:
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, Right click to
createTask2 | Properties | In CorrelationToken enter ReviewStateToken then
Expand it, at OwnerActivityName choose stateReview from dropdownlist
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
OnTaskChanged2 | Properties |, set CorrelationToken is ReviewStateToken from
Dropdownlist then expand it and set OwnerActivityName is stateReview
Next, set TaskId by
click button … follows as
onTaskChanged2_AfterProperties1
= onTaskChanged2.AfterProperties;
onTaskChanged2_BeforeProperties1 =
onTaskChanged2.BeforeProperties;
Next, drag and drop
IfElse control in Toolbox to below onTaskChanged2 follows as:
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, click tab
Workflow1.cs (code) and paste code to here
{
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:
Next, UI as follows then
drag and drop SetState control in Toolbox to below ifElseBranchActivity6
follows as:
Open your site | Click
Shared Documents and add any document
Next, enter % Complete
is 100, special when comment is “Approved” in Description is workflow completed
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.
Great article, I almost forgot the workflow that i had built few years back, but by looking on to your blog, it reminded me everything.
ReplyDeleteThanks Bro
Thanks Great Abhi very much
ReplyDeleteI just learned SharePoint 2010. So i revised all processes... Thanks for writing such informative Blog.
ReplyDeletegetting "File removed for violation" error when clicking on Project link and also couldn't load all images in this blog. please check.
ReplyDeleteThanks Niteen Badgujar,
ReplyDeleteLet me check project link, but my all images display well, please check.
Thanks again
Superb Article
ReplyDeleteJean Paul
www.jeanpaulva.com
Thanks Jean Paul very much...
ReplyDeleteThanks good post....I just want to know that how to delegates user if a person is not available for approval
ReplyDeleteThanks for reply, if person is busy for approval you can user if person busy then assign to (delegates) another user in your code. depend on your business. however, how to know the person busy or not, you can add new Field "status" with value Busy and free.
ReplyDeleteThanks.
Gr888888.....
DeleteCan you send the code to mailpravinat @ gmail
ReplyDeletehi mailpravinat@gmail, i uploaded my project on top article, you can donload it.
DeleteThanks.
Thanks for this information. Can you give some tips on how to use Workflow in SharePoint 2013? I have just signed up for a free SharePoint 2013 site with http://www.cloudappsportal.com
ReplyDeleteNice tutorial on workflow
ReplyDeleteThanks navneet sharma.
ReplyDelete