Welcome to MSDN Blogs Sign in | Join | Help

Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

The Workflow Objects

Workflow Template vs. Association vs. Instance

There are a few important terms in workflow that often get confused: template, association, and instance. If you don’t read anything else from this series, make sure you understand this section. Keeping these terms straight is critical to understanding other documentation and the pieces of a SharePoint workflow.

First we have a template. The template is the SharePoint workflow "Feature" that is deployed to a site collection (a Feature is a custom SharePoint part, such as web parts or content types). The template basically says, "Hey, this workflow feature uses these forms and this assembly." It’s just a skeleton stored in the central repository for a site collection that any list or content type can look at.

But it’s just for looking; a list can’t use the template directly. Should the workflow be started automatically? Where should tasks be stored? We need an extra layer to customize a template with this information. Enter the association. To connect the list to the template, we create an association. An association stores customizations for the template and binds it to a list or content type. Such customizations can include a title for the association, when to start running the template (e.g. manual or automatic start), and custom workflow settings like default approvers. The important thing to understand here is that a list or content type can have multiple associations of the same template. For example, you can have associations Expense Approval and Publish Approval that that bind to the Approval workflow template, each customized with their own settings and default values; they should just be named differently.

When we actually start a workflow, we create a workflow instance of an association (not a template). Each item can run one instance of a particular association at a time. So there can only be one ExpenseApproval instance running on each list item, and one Publish Approval instance. But both are based on the Approval template.

So the workflow that you develop in Visual Studio is a workflow template that defines the general feature. The Association form that it specifies collects customized association data to create the association. The Initiation form collects data to create a new instance from an association, and uses the association data to pre-populate initial values.

Tasks, History Items, and Modifications

The other important workflow-specific SharePoint objects are the workflow task, workflow history item, workflow modification.

Once a workflow is running, it can call any SharePoint object model code. Because SharePoint workflow focuses on human-based process rather than automating purely programmatic steps, an important action for workflows is creating tasks on a SharePoint task list specified in the association. Workflow tasks are like normal SharePoint task items, but they also link to the workflow that created them and can notify their "parent" workflow when they change or get deleted so that the workflow can take appropriate action. Users can edit a workflow task by using a task form (also specified in the template), and the workflow can process these edits and mark the task as complete. Since part of the benefit of having workflows is to gain visibility into a process, the workflow can log events to a workflow history list. History items are just list items that have special columns for workflow-specific data, such as instance id, association id, etc. These items appear on the workflow status page, which filters according to instance id.

But as with any process, sometimes a workflow needs to be adjusted while it in progress, for example, if a new person needs to be added, if a due date needs to be changed, or if a parameter, such as a "max limit" for an expense report that determines whether the report requires manager approval, should be reduced in-flight. To allow users to talk to or change workflows in progress, SharePoint has workflow modifications. The modification object represents a channel of communication with a workflow, and once registered with a workflow, can "wake" a workflow up when the modification is called. A modification form collects data required for the modification, and once the workflow is awake, it can perform whatever modification the developer programs. In general, use modifications if you need to talk to a running workflow and don’t want to do it through tasks.

Understanding these fundamental concepts in SharePoint workflow will help you understand how all the pieces fit together when you create your workflows. To summarize, here is an application of these terms in the workflow life cycle:

  1. Developer develops a workflow template.
  2. Server box administrator deploys the template to a site collection.
  3. List administrator associates a workflow template with a list or content type on that site collection to create a new association.
  4. User starts an instance of a workflow association on an item (or workflow instance starts automatically on event).
  5. Workflow executes, perhaps creating some tasks and history items on the Tasks and Workflow History lists specified in the association.
  6. Workflow owner (the person who started the workflow) or list administrator can talk to a running workflow at any time by modifying it with a modification.

David Chappell has a great whitepaper on MSDN that describes the structure in more detail, so if you’re interested, I’d highly recommend it:).

 

Crash Course on Workflow Mechanics

It’s not critical understand the low level details of how workflows operate internally to develop workflows, but there are a few the important high level concepts that will help:

  1. When running workflows reach a point in the code, a.k.a. schedule, where they need to wait for something to happen (such as a task edit), they are serialized (i.e. turned into binary) and dehydrated to the SharePoint database. This means that the workflow object is turned into a string and stored to the database. We can say that the workflow is asleep.
  2. Dehydrated workflows are no longer in memory.
  3. When the event the workflow is waiting for happens, the workflow is deserialized and wakes up, or rehydrates, then continues where it left off in the schedule.

A typical SharePoint workflow is a series of "execute custom code, dehydrate, and rehydrate" cycles. Dehydration and rehydration requires the use of workflow services.

A local service is a channel of communication between SharePoint and the workflow. The important thing to know about them is that they can manipulate workflow items and register workflows with SharePoint events, put workflows to sleep to wait for the events, and wake up workflows when those events fire.

The SharePoint workflow service APIs provides both methods and event handlers. Methods will perform actions, and handlers will put the workflow to sleep and wake up on events. For example, using the CreateTask service method will create a task and register the workflow for events on that task. Using the OnTaskChanged service event handler mapped to that task will dehydrate the workflow while it’s waiting and rehydrate the workflow when the task is changed.

To map these functions to the same task, the services use correlators, unique identifiers that the workflow engine can use to map events and actions to objects. These are specified in a workflow by using correlation tokens and GUIDs. (Both correlation token and GUID are required.)

Ok, so that was really dry. But basically, the key thing to know is that you must use the service methods to get the workflow to respond to events on items. The other thing to know about service methods is that they’re transacted actions, batched actions that are not committed until the workflow is dehydrated. This means that if you use CreateTask, the task will not actually be created until the workflow is successfully dehydrated.

Pitfall: Operating on uncommitted items
Do not use SharePoint object model (OM) calls on tasks or items that have not been committed. Remember to dehydrate first, using an event handler or delay, before trying to fetch objects. For example, if you use CreateTask, the SPListItem for the task will not exist until you use an event handler like OnTaskCreated or Delay to put the workflow to sleep. Using other workflow service methods is ok, but don’t use direct OM on the SPListItem object before putting the workflow to sleep.  

 

-Eilene Hao, Program Manager, SharePoint Workflow

Published Saturday, November 18, 2006 8:22 PM 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

# Developing Workflows in VS: Part 1 - MS SPT blog

Monday, November 20, 2006 6:19 AM by All About SharePoint

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

What about SharePoint Designer???  When and where will it be RTM'd.  I'ts not part of any of the Office 2007 suites:(

Monday, November 20, 2006 3:13 PM by BobC

# Please release Office Sharepoint Server 2007 on eOpen downloads Volume licensing site!

If there is any way to push to have this put up on the volume license site which is accessed through the eOpen site it would be mucho appreciated!

I am looking to deploy a server right away and I have the license but Sharepoint Server hasn't been made available yet.

Interestingly enough, BobC (the previous commenter) may be interested to know that Sharepoint Designer has been released and does appear for me on the download site associated with eOpen.  

Now I just need SharePoint Server to show up as well!

Monday, November 20, 2006 5:11 PM by spfreak

# MOSS 2007 on eOpen downloads please

I noticed Groove Server was put up but no MOSS :\

I guessed at what the url would be for the MOSS download based on the url for Groove Server and guessed correctly!

I was so excited that I might be able to finally grab MOSS RTM but alas even though it looks like the page has been setup the actual file was not present.

https://www.microsoft.com/licensing/servicecenter/downloads/ProductDetails.aspx?pcode=OFPSPS07&pfcode=OFP

above link works but download file which appears to be named 'Office SharePoint Server 2007 (X12-34619).IMG' was not found :\

(entire download link is: http://vl.download.microsoft.com/dl/download/release/Office%20SharePoint%20Server%202007%20%28X12-34619%29.IMG?lcid=1033&__gda__=1164125573_fbeb08b9748e0954065e2562dd92b314 )

If there's anyway to expedite posting the file it would be tremendously appreciated!

Monday, November 20, 2006 5:30 PM by spfreak

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

SPFreak (love the alias!), MOSS 2007 should be available on MSDN Subscriber Downloads as well as the Volume License Downloads by Nov. 30th.

<Lawrence />

Monday, November 20, 2006 5:42 PM by LLiu

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

This is fantastic information. We need much more of this kind of information. Knowing the reasons behind the "cookbook steps" makes it much easier to understand and comprehend. Please continue providing more of this "under the covers" type of information!

Monday, November 20, 2006 8:25 PM by jimmiett

# Link Listing - November 20, 2006

WPF/E CTP Around the Corner [Via: Jesse Ezell ] New and Notable 126 [Via: SamGentile ] Getting this...

Tuesday, November 21, 2006 12:31 AM by Christopher Steen

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Great thread, Eilene! Can you address the following topics at some point during this series?

* potential pitfalls relating to simultaneous workflow instances on the same item

* how workflow instances and item versioning interact (and related pitfalls, if any)

Thanks!

Tuesday, November 21, 2006 9:34 AM by BAG

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Thanks for this series and this post in particular; I find it very helpful to have an authoritative definition of terms as it facilitates conversation and staves off confusion.

Tuesday, November 21, 2006 12:07 PM by Douglas Ryan VanBenthuysen

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hey BAG,

The only major pitfall I can think of for having multiple workflow instances running on an item are if they manipulate the same columns and may step on each other's toes. For example, if you have a workflow A that sets some approval bit on an item, and another workflow B that sets the same bit, B may overwrite the value that A put in.  So when you plan your workflow and which list it's going on, make sure you understand what kind of conditions like this exist and whether or not they are ok.

In general, workflow doesn't have any special interaction with versioning; workflows just run on the latest item, kind of independently of the version.  So if you save a new version of the document while a workflow is running, the workflow doesn't change (unless you programmed it to respond to item changes in general).

The special case for workflow and versioning is the content approval setting for workflows on lists with major/minor versioning enabled. (i.e. on association, you can check the "Start when a major version is published" box. Note: Only one workflow can be the chosen one for a list;))  This will take you to an initiation page when a version is published, and you can start a workflow thru the initiation page.  If the workflow is already running, publishing a new version will cancel the workflow.

Hope this helps.  Let me know if you have more questions.

-E

Tuesday, November 21, 2006 3:28 PM by Eilene Hao

# Developing workflows in WSS 3.0 inside Visual Studio

One of the big features in WSS 3.0 includes their support for workflows (they have a new dependency on

Saturday, November 25, 2006 6:34 AM by James Manning's blog

# 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

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

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

I've read your posts about dehydrating and rehydrating a workflow instance and I created my own custom very simple workflow with a log activity and a Delay.

The problem is that once the instance is serialized it never wakes up again. Even setting its sleep durantion for 1min it will still in progress after hours until it be canceled.

Maybe it lacks something to be setup to fire the wake up event or something like that but I few that it is not right.

Is there a service or anything else that should be in place so the event gets fired and my workflow instance wakes up and completes successfully?

Wednesday, November 29, 2006 5:12 AM by Carlos Cardoso

# 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:34 AM by Romeo Pruno

# OnePage Workflow in SharePoint 2007

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

Tuesday, December 05, 2006 2:37 PM by SharePoint, SharePoint and stuff

# 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:24 PM by Bob Fox's Sharepoint Blog

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

There's a bug with delay; we're working on a hotfix...sorry for the inconvenience!

Friday, December 08, 2006 8:37 PM by Eilene Hao

# 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&ccedil;&atilde;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 1 - Workflow Objects and A Crash Course on Mechanics

Hi Eilene,

We're a Gold Partner using the WSS v3 RTM on a customer project and we have a big problem with the workflow not rehydrating after a Delay.

Do you have a proposed date for the hotfix?

Regards,

James

Monday, January 08, 2007 11:31 AM by JamesOrton

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hi:

I am struggling with given tasks a unique set of permissions.

Basically, my workflow assign a task to a certain persion. This task is being added to the "Tasks" web part.

In my code of the workflow (workflow.cs) I want to manupulate the task permissions so that only certain people will be able to view the task.

How do I do that?

Monday, January 08, 2007 8:15 PM by Tolga Koseoglu

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

how do we change the display icon of a document library item(e.g word doc)when a workflow is started on it..i have seen the pending approval image in the images folder but do not know how to incorporate it....i need to change the icon to signify that a workflow is running on it..something like the checkout icon which is displayed when an item is checked out

Wednesday, January 10, 2007 6:00 AM by keith rebelo

# is there any Visual studio sharepoint server workflow Video

I am asking about any advance video for visual stuio workflow for sharepoint server.

Thursday, February 15, 2007 5:47 AM by khaled

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hi Eilene,

When a tasks is assigned to aspecific user, he should teh only one who can access this task (view or edit teh task). But what is happening is that everyone can access everyone elses tasks even after adding the following lines of code:

this.AssignTask.SpecialPermissions = new System.Collections.Specialized.HybridDictionary();   //AssignTask is the name of the CreateTask activity

this.AssignTask.SpecialPermissions.Add(this.taskProperties.AssignedTo, SPRoleType.Contributor); //taskProperties is the variable bound on the TaskPropertied of the CreateTask  activity

But actually it isn't working. Kindly note that teh workflow is a state machine model.

Waiting your soonest reply,

Thank You,

Zeina

Friday, February 23, 2007 2:39 AM by Zeina

# Is this a bug???

fgonzales@soas.ac.uk

I can create a List with anonynous access enabling users to add new items requiring content approval, this works fine and after adding an item they return to the list page. The problem starts when I add a workflow to the list the new item is added but the user instead of returning to the list page gets an

"An unexpected error has occurred.

Troubleshoot issues with Windows SharePoint Services." Page I removed the workflow and checked the list but now the only confirmation is the above error and so I have to recreate the list from scratch to work.

Any Thoughts.....

Thursday, March 08, 2007 11:34 AM by Felix Gonzales

# Developing Workflows in VS: Part 1 - MS SPT blog

Sunday, March 11, 2007 6:38 AM by All About SharePoint

# Start Learning Workflows!!

Wow such an exciting topic to learn!! its nice and fun yet its a bit difficult to grasp easily,,, This

Thursday, April 12, 2007 9:45 AM by Code Eater

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Regarding the Workflow History Log.

Is it possible to set a condition that looks for the workflow in the history so that things are not done twice?  For example, I have workflow that runs every time an item is changed...wish I could only run it if a field is changed...anyway if a particular field is not blank it causes an update in another list.  But if this is already done - then the result would be bad.

Can I use the history log to check to see if it was done?

Wednesday, April 18, 2007 4:16 PM by GirlGeek101

# call Infopath form on client Side from Workflow

I am totally stuck here....help me plz...

I have a requirment to run a workflow in Sharepoint 2007 where

a task will open Infopath 2007 form on client side Office application.

On submitting the Infopath Form from the client side the WorkFlow will resume

-Thanks a lot in advance

iyermanoj@gmail.com

Wednesday, May 30, 2007 5:26 AM by Manoj Iyer

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hi,

i have problems to run a workflow in Sharepoint server 2007. The workflow is developed in Visual Studio 2005. The compiling and the install procedure are successfull, but when i start the workflow i get an ErrorMessage: "failed on start". Whats wrong? It's an really small workflow, includes a "onWorkflowActivated"- and a "logToHistoryList"-Activity

Kindly suggest a solution

Partha

Monday, July 02, 2007 6:55 AM by apartha77

# 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:12 AM by The Boiler Room - Mark Kruger, Microsoft SharePoint MVP

# Development--

Body: Application Development on MOSS 2007 &amp; WSS V3 Beginning custom SharePoint development with

Tuesday, December 11, 2007 10:54 PM by Mirrored Blogs

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hi Eileen

Any estimates on the proposed date for the delay hotfix would be most appreciated.

Kind regards

Deepak

Thursday, January 17, 2008 12:45 AM by Deepak Maharaj

# SharePoint resources

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

Monday, February 18, 2008 10:20 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

# Sharepoint Workflow developement in Visual Studio

I cannot find anything that spells out whether or not I need to install Visual Studio 2005 on the WSS 3.0/MOSS server or not.  My SysAdmins really don't want VS on the production server and I don't really want to have to ask Management for MORE funds to pay for extra Windows Server AND MOSS licenses for a developer environment.  Its all starting to add up.

I have created development web apps and site collections in MOSS and WSS which function well with SPD but Visual Studio does'nt want to start the SharePoint designer tools and templates without MOSS/WSS being present.  Surely VS does'nt require MOSS or WSS to be present locally.

Wednesday, March 19, 2008 9:31 PM by CA_Dave

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

I have been creating many workflows in Workflow Designer and everytime I use the "Set a field in Current Item" or "Update list item" actions, many instances of that workflow runs upon item creation or edit - like an endless loop of the same workflow.

Anyone ever experience this before?

Thursday, April 24, 2008 8:31 AM by Kandy

# 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:39 AM by Kirk Allen Evans's Blog

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Great thread!

Do you know if it is possible for a workflow to use more than one task form? Obviously each would be registered in workflow.xml but if it is possible, how does the workflow know which task form to use and when?

thanks

Thursday, May 22, 2008 6:03 AM by Richard Jacobs

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

I have a workflow developed and in production.The problem is when i upgrade the workflow by adding new tasks and deploy the new worklfow on the deployment site the already In Progress items dont move forward where as a new item from starting can walk through the changes that i made in the worklfow.

What could be the reason?

Thursday, June 19, 2008 8:07 AM by Redd

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Reply to Redd:

You can not update a workflow, where there are runnin ginstances, because the workflow engine stores binary information about the workflow and if this signature does not match, the workflow starts a new instance of the 'modified' workflow. There is some incomplete information what you have to do in such a case. The base line is, that you have to create a complete new workflow (new GUIDS, new feature, new names) and place this workflow for example with the name Workflow V1.1 on the list or content type. Then you have to set the old workflow in 'no new instances' mode.

Monday, June 23, 2008 5:58 AM by MarkusT

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hi Richard Jacobs

                 ypu can use more than one task form in workflow.

give you task forms urn's in the workflow.xml file as

<Task0_FormURN>urn:schemas-microsoft-com:office:infopath:TskForm1:-myXSD-2008-05-15T04-42-13</Task0_FormURN>

<Task1_FormURN>urn:schemas-microsoft-com:office:infopath:TskForm2:-myXSD-2008-05-15T04-42-13</Task1_FormURN>

In the code:

in create task method invoking

you can use

"createTask1_TaskProperties1.TaskType"  to set which form should be displayed to which user.

createTask1_TaskProperties1.TaskType=0

will refer to first form

when you set

createTask1_TaskProperties1.TaskType=1;

then it will refer to second form.

Friday, July 11, 2008 9:33 AM by chandrika

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Thnx for the reply MarkusT.

I knew abt this workaround but there are many such In Progress Tasks on the production site and we need to make changes to the workflow.We should also keep in mind that the older In Progress items use the changes.

I am havin a try on assembly redirection which may break this jinx

Thanks any ways

Tuesday, July 15, 2008 2:32 AM by Redd

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Redd, I have exactly same problem. I can't go to tasks generated by previous version of workflow. But my new ones are moving find.

I am still not sure what I can I do. Please share me if you find solution.

Thanks, Vuthy

Tuesday, July 15, 2008 11:45 PM by Vuthy

# re: Developing Workflows in VS: Part 1 - Workflow Objects and A Crash Course on Mechanics

Hi Redd, I think I have the same problem. My workflow is in production. There are some instances in progress. I then have a need to upgrade it to newer version. I did change GUID and name but still after deploy it, I can't access tasks created by previous version. I get "Unknow Error" while clicking on those tasks. I am still trying to solve it. Please share me if you did solve your issue.

Could anyone else through some light?

Thanks, Vuthy

Wednesday, July 16, 2008 2:07 AM by Vuthy

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker