Welcome to MSDN Blogs Sign in | Join | Help

Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Okay, the moment you’ve all been waiting for: how do you write a custom SharePoint workflow?  In this section, we will break it down into five steps:

1)      Model your workflow in Visual Studio

2)      Create and bind your forms

3)      Code your workflow

4)      Deploy your workflow to the server

5)      Debug

(Note that these are only suggested steps; you can do this any way that fits best for you.)

Step 1: Model Your Workflow

The first step is to model your workflow using the Visual Studio Extensions for Windows Workflow Foundation designer, i.e. create a new workflow project, drag and drop activities into your workflow surface, and set the properties that you need.  Having a skeleton of your “flow” will help you see what needs to be done and where you will need to bind your form data in later steps.  Also, initializing the properties on your activities up front may save you some time having to switch back and forth between designer and code-behind file when you start writing your code.

FAQ: The workflow template can’t open in Visual Studio
When using the SharePoint workflow VS templates, you may encounter an error opening your .csproj.  This usually means that you do not have the Visual Studio Extensions for Windows Workflow Foundation installed.  Check to see if you have anything under the “Workflow” node before you worry about the “SharePoint” node.

SharePoint Activities

SharePoint comes with a set of activities.  Many of them wrap the service methods mentioned in the mechanics section above, or rather, they are the service APIs in activity form.  The method activities are blue, and the events handler activities are green.  The methods derive from the WF CallExternalMethod activity (see Item Activities sample in the ECM Starter Kit), and the functions you write for the Method Invoking property are executed right before the service method executes.  The handlers derive from the HandleExternalEvent activity, and the functions you write for the Invoked property are executed after the service handler executes and wakes up the workflow. 

Add an event handler activity whenever you want the workflow to commit the method actions or go to sleep, then wake up when an event fires. 

To create and bind variables to your activity properties, use the activity property dialog by clicking on the blue icon next to the property.  If instead of clicking on the blue button, you type in a value directly, for example, typing 1 for an int property, the property will be set to that constant value. (It’s less expensive for performance to do this if you know a value won’t change)

Pitfall: Using CreateTask or OnTaskChanged’s SPWorkflowTaskProperties objects as a real time representations of the SharePoint task item
When using CreateTask, the object that you bind to the TaskProperties property to set initial data for the task is only initial data; it’s not synchronized with the task, so it will not reflect any changes to the task, or vice versa.   The same concept applies to OnTaskChanged’s AfterProperties property.

The Importance of Correlation Tokens

A common mistake when initializing properties is not setting the correlation token correctly to map different activities to the same object.  If not done correctly, the workflow will not wake up or respond to events. 

Think of a correlation token as an identifier that WF uses to identify and respond to the corresponding objects in SharePoint.  For example, if you have in a sequence the activities CreateTask, OnTaskChanged (to wait for the user to edit his task), then CompleteTask, you want these three activities to map to the same task in SharePoint; you do this by specifying the same correlation token in all three. 

You will need a token for activities that relate to the workflow itself, a token for activities related to a particular task or item, and a token for each modification.  Here is a grouping of activities that need the same correlation token (each group should have its own token):

“Workflow” token:

  • OnWorkflowActivated
  • OnWorkflowItemChanged
  • OhWorkflowItemDeleted
  • SetState
  • SendEmail
  • UpdateAllTasks

Task token:

  • CreateTask
  • CreateTaskWithContentType
  • UpdateTask
  • DeleteTask
  • CompleteTask
  • RollbackTask
  • OnTaskChanged
  • OnTaskDeleted
  • OnTaskCreated

Modification token:

  • EnableWorkflowModification
  • OnWorkflowModified

 

 

Missing from this table are the list item service APIs, which don’t have activities around them in the box.  But if you use wrapper activities for them (such as those included in the ECM Starter Kit item activities sample), you would treat them similarly to tasks: every related item activity should use the same correlation token.

Pitfall: Using the workflow token as the task token
Sometimes developers use the workflow token as their task token; now all tasks map to the same thing, right?  This isn’t quite correct. Make sure each group and object has its own token.  For example, don’t use the overall workflow token for your tasks.  Also, create a separate token for each distinct task.  Note: if you have a task in a sequence inside the replicator activity, changing the “Owner Activity Name” to the sequence will automatically create a new token for every instance of the sequence.

 

Next on Developing Workflows in VS: Design and Bind Your Forms

Thanks!
Eilene

Published Thursday, November 23, 2006 9:00 AM by sptblog
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Morning Eilene,

I was just sitting here eagerly awaiting your next installment, and then it appeared!

Many thanks!

Thursday, November 23, 2006 4:20 AM by Kathryn

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Same here.  I was too anxiously waiting for the article and putting my feedback in article 2 and after submitting see that artible.  I have a question here

Will ECM Starter Kit for Microsoft Office SharePoint Server Beta II TR will work with Office SharePoint Server RTM?

What is the difference between the workflows in Visual Studio.Net 2005 and Worfklows in SharePoitn Designer 2007 technically and theoratically?

Regards,

JERRY

Friday, November 24, 2006 1:30 AM by Jerry

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Hi Jerry, The ECM Starter Kit will work for the most part, although a few of the samples have been revised for RTM, and the RTM kit will include more:).  

I'll cover this in the appendix later on;), but here are the key differences between the two:

SPD is geared toward the Web Designer/Business admin.  It's easy to learn, and you don't have to write any code.  You can put together a lot of workflows with just sequence of actions and conditions.  The other cool thing about SPD is that you can deploy remotely (vs. VS, where you have to deploy on the server box)

However, there are a few limitations (deploys to a list at a time, no IP forms, only sequential workflows). So you will want to use VS if…

• You need to create a workflow that can be deployed to all lists in a site collection.

• You want to use InfoPath forms for your workflow

• You need more actions than the ones available by default in SPD

• You want to use state machine workflows

Friday, November 24, 2006 1:49 PM by Eilene Hao

# Workflowentwicklung für SharePoint

Gestern hat das SharePoint Products und Technologies Team den vierten Artikel einer Serie zur Entwicklung

Monday, November 27, 2006 2:37 PM by XML is the glue...

# OnePage Workflow in SharePoint 2007

OnePage : der einfache Blick auf das Wesentliche - die wichtigsten Informationen zu einem Thema auf einer

Monday, November 27, 2006 4:45 PM by SharePoint, SharePoint and stuff

# OnePage Workflow in SharePoint 2007

OnePage : der einfache Blick auf das Wesentliche - die wichtigsten Informationen zu einem Thema auf einer

Tuesday, November 28, 2006 4:11 AM by SharePoint, SharePoint and stuff

# Adding CreateTask Activity to a SharePoint Workflow

Wednesday, November 29, 2006 10:58 PM by Robert Bogue [MVP]

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Eilene,

Is there any documentation around explaining how, exactly, the correlation tokens are used?

For example, all the Sharepoint Task Activities (CreateTask, OnTaskChanged, etc) require a correlation token.  The also require a GUID named "TaskId".

How are these related?  Will the same task always use the same correlation token?  Which one(s) do I set, and which are set for me?  What exactly would happen if two activities had the same TaskId but different CorrelationTokens?  Or vise-versa?

If there's a one-to-one mapping between CorrelationTokens and TaskIds, why not just use the TaskId as the token?  I'm sure there's a reason, but it's never been explained.

Saturday, December 02, 2006 11:44 PM by Daryl

# Implementare un workflow con Sharepoint

Visto che mi avete chiesto info a riguardo vi giro i link alle prime 7 lezioni: Lezione 1 Lezione...

Monday, December 04, 2006 6:35 AM by Romeo Pruno

# Creating Workflows with Visual Studio 2005 (Walkthrough)

Im not sure how many have found this yet but Eileen Hao has put together a Seven Part walkthrough with...

Thursday, December 07, 2006 12:25 PM by Bob Fox's Sharepoint Blog

# WF inside of SharePoint

I'm a little late to notice this, but Eilene Hao, a PM on the SharePoint Workflow team, has put together

Wednesday, December 13, 2006 4:51 PM by Matt W's Windows Workflow Place

# SharePoint 2007 workflow development

Encontrei esta compilação interessante de recursos sobre desenvolvimento de workflows para

Saturday, December 16, 2006 8:23 PM by Miguel Isidoro

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Hi,

I dont want to use CreateTask Activity in my workflow. Instead of this i want to create task programmatically.

How can i achieve this.

Your comments will be helpful for me.

Thanks.

Wednesday, April 04, 2007 9:16 AM by Prashant

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

When creating a custom email body for a task's assignment (CreateTask.MethodInvoking), can I access the id to pass into the task's edit page? The id would be for that particular assignee.

Wednesday, April 04, 2007 10:47 AM by MAndrade

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

I added a replicator activity to my workflow, inside the replicator, I added a CallExternalMethod Activity which updates a List Item.

There is no UntilCondition specified, which implies that the child activity should execute and the replicator should end. But this does not work. The replicator does execute the CallExternalMethod activity.

Any ideas???

I tried adding a sequence and the callexternalmethod inside the sequence with the proper OwnerActivityNAem(tht of the sequence). But still it does not execute the child activity.

Friday, April 13, 2007 3:14 AM by Amol

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Regarding correlation tokens...I've got a fairly simple workflow that I created. Basically all it does is:

1) Create a task

2) Waits for the task to complete (in a WhileActivity)

3) Once the task is complete, wait for the workflow item to update, using an OnWorkflowItemChanged activity inside a WhileActivity

4) Done

The CreateTask and OnTaskChanged activities are all using the same correlation token ("taskToken", not the default "workflowToken").

The OnWorkflowItemChanged activity is configured with the "workflowToken".

What I'm finding is that if at some point between creating the task and completing the task I go an edit the workflow item, the workflow gets into an odd state where it will not progress any further (i.e. if I then complete the task, it will be stuck there).

I assume this is just a case of using the wrong correlation tokens but I'm doing exactly what is suggested in this blog posting ("The Importance of Correlation Tokens").

Any ideas?

Wednesday, June 13, 2007 7:35 PM by H.Y.

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

I create a workflow that implement these function:

-Parameter: SPFieldUserValueCollection to initialized replicator

1) Create a task

2) Waits for other task completed

3) Once the task is complete, wait for the other task completed, using an OnTaskChange activity inside a WhileActivity

4) Done

The CreateTask and OnTaskChanged activities are all using the same correlation token ("taskToken", not the default "workflowToken").

But I get problem.

My WF create tasks, but no jump to OnTaskChange Event. And at my CustomList, status of column workflow is "Completed"

Please help me!

Monday, July 02, 2007 11:24 PM by dungtv2

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Regarding correlation tokens...I've got a fairly simple workflow that I created. Basically all it does is:

1) Create a Replicator activity, Create a task for each user

2) Waits for the task to complete (in a WhileActivity)

3) Once the task is complete, wait for the other task completed, When all Task completed --> Done workflow.

But I get some problem:

- I set debug in OnTaskChange function, but program doesn't jump in. I can't catch event OnTaskChange. I set CreateTask, OnTaskChange activity with the same Token key TaskToken (different with main WorkFfow token).

-I set CreateTask and OntaskChange in SequenceActivity, and set this Composite activity in Replicator activity!

Please help me! I used to many effort but can't sole this problem!

Tuesday, July 03, 2007 10:09 PM by dungtv2

# How to customize the workflow task expiration email

Hi,

Would you please give me some suggestions on how to customize the default notification email when a workflow task is overdue?

Monday, July 09, 2007 9:50 PM by LL

# 2007 MOSS Resource Links (Microsoft Office SharePoint Server)

2007 MOSS Resource Links (Microsoft Office SharePoint Server) Here is an assortment of various 2007 Microsoft

Wednesday, September 12, 2007 11:13 AM by The Boiler Room - Mark Kruger, Microsoft SharePoint MVP

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

I think the part of the article about correlation tokens is still largely vague. IE: "Sometimes developers use the workflow token as their task token; now all tasks map to the same thing, right?  This isn’t quite correct. Make sure each group and object has its own token.  For example, don’t use the overall workflow token for your tasks.  Also, create a separate token for each distinct task"... Your telling people to do something differently but without the 'why' explained! I would love to see a lower level explanation.

I would also rather like to see an example on where we would use different workflow tokens within the same workflow.

Tuesday, September 18, 2007 9:20 AM by Mike Morawski

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Hello All,

I am having a problem with the sendemail activity. When the sendemail is in a serial sequence it works fine. But when it is in a parallel branch it does not work rather gives an error.

Please Help.

Wednesday, October 31, 2007 2:53 AM by Tina

# SharePoint resources

Gracias a Mark Kruger (SharePoint MVP) por esta lista de recursos de SharePoint donde podréis encontrar

Monday, February 18, 2008 10:21 AM by SharePoint mola

# SharePoint resources

Gracias a Mark Kruger (SharePoint MVP) por esta lista de recursos de SharePoint donde podréis encontrar

Monday, February 18, 2008 11:08 AM by SHAREPOINTBlogs.com Mirror

# Thanks to Lexis Nexis!

We are about to start a great day showing technologies like .NET 3.5, Visual Studio 2008, and SharePoint

Wednesday, April 30, 2008 7:40 AM by Kirk Allen Evans's Blog

# Can we use OnWorkflowItemChanged Activity inside Replicator Activity

Can we use OnWorkflowItemChanged Activity inside Replicator Activity?

Thursday, July 03, 2008 12:27 AM by Sam

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Hi,

Say i have 2 states in a state machine workflow.

On completing a task in state 1 creates two tasks one from state 1 and the other from state 2.

The problem that i am facing is thet i am able to complete the task from state 2 but not the new task that was created from task 1

I believe since we are moving to a different state which is state 2 we cant complete the task from the state 1

Any thoughts????

Friday, July 18, 2008 8:55 AM by Chandra

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

I have a OnWorkflowItemchanged activity in a  State Machine Workflow where some code is executed when the this method is invoked followed by another activity for sending out the mail.

Although in this activity I am checking for a flag to prevent repeated execution as the items are saved and changed, the mail is going out 3 times eveytime a change is made to the document.

Please suggest....

Wednesday, August 06, 2008 8:12 AM by NewToSP

# SharePoint Workflow Guidance and Best Practices

I had a question today about SharePoint Workflow "Dos and Don'ts" and I thought it might be a good idea

Thursday, August 14, 2008 1:23 PM by SharePoint Content and Resources

# re: Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

The fact that custom/composite activities don't have a reference to their parent workflow token really limits us as to what we are able to do.  

If I create a custom activity that has a CreateTask (say, for example, to use in a replicator), that Task has really no knowledge of the Workflow it's running within.  This causes some significant problems such as:

*  No knowledge of what Task List the task is being created on.  We know the ID...but not the list?!?!

*  No reference to the Web, Site, Item, or Item List.  How can our custom activity do any actions against a document or list entry if we can't find out what it is?!?!

Additionally, the SendEmailActivity, according to your chart above, needs the workflow token, which isn't available in a custom activity.  Does this mean that sending email isn't available from there?

No offense, but this stuff is really cool when it works, but I can't help but think that the underlying architecture wasn't very well thought out...just my opinion...

If I'm missing something--some way of referencing the parent workflow, I would really appreciate the information!  I've spent three weeks now on the same workflow trying to work around this limitation.

Tuesday, September 16, 2008 12:46 PM by Daniel O'Brien

# 应用SharePoint的Workflow须记住的10句话!

1)SharePoint的工作流是基于文档的工作流,只能关联到列表,文档库和内容类型上 2)SharePoint的工作流模板利用网站功能(Feature)来发布 3)SharePoint的...

Tuesday, September 23, 2008 11:12 PM by 晃晃悠悠

# 微软sharepoint产品组博客——使用VS进行工作流开发系列博客

最近的两个月一直在研究SharePoint的内容,从配置到开发,从配置,到sharepoint对象模型,webpart,eventhandler,workflow,contenttype,自定义字段...

Monday, November 24, 2008 12:34 AM by virus

# 使用VS进行工作流开发系列博客4-Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow

Monday, November 24, 2008 2:22 AM by virus

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker