You are on page 1of 26

Module 9: Workflow


 Workflows in Microsoft SharePoint Products and

 Workflow Life Cycle
 Building Custom Activities
 Building Custom Workflow Templates by Using
Microsoft Visual Studio
What Is a Workflow?

 Long Running : Days, Weeks, Months, Years

 Serializable: Doesn’t have to be running the
whole time
 Resumable: Can be restarted when necessary
SharePoint and Workflow

 SharePoint is a:
 Persistence Engine – You don’t have to worry
about where the workflow is stored
 User Interface – Interact via the Web or
through Microsoft Office system integration
 Workflow brings process to SharePoint data
Differentiating Event Handlers and

Event Receiver Workflow

 Lightweight  Robust
 Short Running  Long Running
 Automatic Initiation  Automatic or User
 No User Interface Initiation
 Optional User Interface
Non-Developer Workflow Options

 Out-of-the-Box Templates
 Three Templates: Three State, Approval, Collect Feedback
 Easy to use but flexibility is limited
 SharePoint Designer
 Easier to build than workflows with Visual Studio
 Limited rules-based engine
 Can not be moved between sites or between environments
(not reusable)
Workflow Life Cycle

Developer wraps Farm administrator
develops a workflow template in deploys the workflow
workflow Feature and Solution solution

User or event Administrator creates A Site Collection

a workflow administrator
starts a workflow association with a activates the
instance list or content type workflow feature

Workflow executes
Optionally, the
activities and
workflow owner Workflow
optionally creates
tasks and history
modifies the running completes
workflow instance
Workflow Forms

 Association – When the workflow template is

associated with a list
 Instantiation – When a workflow instance is
 Workflow Modification Form – When the user
wants to modify the flow of a running workflow
 Workflow Status Form – The form displayed to
the user to indicate the status of a workflow
Content Types and Workflows

 Workflows can be associated with a content type

 Workflow Tasks are a special content type
 Content Types can have special new, display,
and edit forms
 Therefore, the task edit page for a workflow
task can be customized
Development Environment

 Microsoft Visual Studio 2005

 Windows Workflow Foundation Extensions for the
design experience
 SharePoint Workflow Project templates
 Microsoft Visual Studio 2008
 Microsoft Visual Studio Tools for Microsoft Office
2008 delivers a workflow project template
Automates deployment and activation
Components of a Workflow

 Simple Activities – Does something

 Composite Activities – Coordinates other things
 Custom Code (via Simple Activities) – Does
everything else
Simple Workflow Activities

 An activity is:
 An atomic set of instructions used to
complete a unit of work
 A reusable component used to
compose workflows
 Activities are like controls in forms
 You drag and drop them onto a
design surface
 You modify their properties with
property sheets
 You generate event handlers and
write code inside
Common Composite Activities

 If Else
 While
 Sequence
 Parallel
Writing Code in the Workflow

 Code Activity
 Contains one method to execute—thus
providing a way to plug code directly into a
 Custom Developed Activity
 Derive from the Activity class, override the
Execute method, and deploy to the Global
Assembly Cache (GAC)
The Workflow Project

 Sequential Workflow
 Predictable and fixed path for the workflow
 Move from one activity to the other, possibly branch but
flow is predefined
 State Machine
 Event-driven workflow
 Events control the
execution of the
Model the Workflow

 Drag and drop activities on the design surface

 Service activities
 Event activities
 Code activities
 Activities to branch
the workflow

 Delivers SharePoint context information to your

workflow code
 Property bound to WorkflowProperties property of
OnWorkflowActivated event activity
 Access item information
public SPWorkflowActivationProperties workflowProperties =
new SPWorkflowActivationProperties();
public double TotalAmount = 0;
private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
TotalAmount = Convert.ToDouble(workflowProperties.Item["Total"]);

 Process association and initiation data

Branch the Workflow

 Define Rules
 Code condition
 Declarative condition
Code Activity

 Basically empty activity

 Connect your event handler
executing custom code

private void SetApproved_ExecuteCode(object sender, EventArgs e)

workflowProperties.Item["ReportStatus"] = "Approved";
Model of the Workflow

 CreateTask activity
 WhileActivity
 OnTaskChanged
 CompleteTask
Preparing a Task

 Item based on Workflow Task content type

 Inherits from Item content type
 Additional fields to store workflow information
 Event handlers
 Hook up your own code with the event handlers and
 Assign an ID
 Set the properties of the task item
private void createTask1_MethodInvoking(object sender, EventArgs e)
Task1Id = Guid.NewGuid();
Task1_Properties.Title = string.Format("Expenses of {0} to Approve",
string managerEmail = workflowProperties.Item["ManagerEmail"].ToString();
string manager =
Task1_Properties.AssignedTo = manager;
While Loop

 Code condition
 Class-level variable in code is set to true if
manager completes the task (setting
percentage complete to 100%)
 Exit the loop when condition is false

private void IsWaiting(object sender, ConditionalEventArgs e)

e.Result = !taskCompleted;
Workflow Forms

 Forms are used for configuring, initiating, and modifying

workflows and completing tasks
 Association Form, Initiation Form, Modification Form, and Task Edit
 ASPX Forms
 Can be used by Windows SharePoint Services and Microsoft Office
 Server-side only
 InfoPath Forms (SharePoint Server or Microsoft Office Forms
 Server rendered by Microsoft Office Forms Server
 Microsoft Office client integration
 Write once, run server and client (same form)
ASPX Pages

 Replace Windows SharePoint Services ASPX pages

with custom ASPX pages in workflow feature
<Workflow Id="4797A6A6-4F31-40ca-9814-746402C2DB56" Name="Litware Approval"
Description="Sample workflow template demonstrating workflow input forms"
CodeBesideAssembly="LitwareWorkflows, Version=, Culture=neutral,
TaskListContentTypeId="0x0108010084565D92BEFE4a75A28C2F658B7BECCA" >
Modify workflow instance with a custom Litware form
Typical Flow of Creating ASPX

 Create ASPX with code-behind

 Note that you have to manage all of the
interaction between your page, Windows
SharePoint Services, and the workflow engine
 E.g., Creating the association, starting the
workflow, serializing the data
 Deploy ASPX in 12\Template\Layouts folder

 Workflows in Microsoft SharePoint Products and

 Workflow Life Cycle
 Building Custom Activities
 Building Custom Workflow Templates by Using
Microsoft Visual Studio