Writing a custom workflow for SharePoint 2007 with Visual Studio 2005

After spending days trying to get my custom workflow to work, I’ve finally got there and as always, it’s the tiny mistakes that cause major failures.  The key thing I’ve learnt from this exercise is tobe very careful when producing XML files.  As I’ve discovered, Workflow production is ultra Case Sensitive and the slightest fault will throw errors after deployment.
 
This may become a long blog posting, but should help developers as a useful reference when creating a Custom Workflow.
  
The solution files for this walkthrough can be downloaded from the SharePoint Team Project within Team Foundation Server
 
Introduction
 
We are going to create a sequential workflow (a workflow that typically has several predictable paths of execution that can be modelled in the form of a flowchart) and tie it to a custom list with one column – ‘Title’.
 
When activated, the workflow with change the title of the list item to a numberbetween 1 to 6 randomly, much like rolling a dice.
 
Through this step by step walkthrough, I will demonstrate how to create this workflow and most importantly how to deploy and debug a workflow successful. You will need to perform the following steps to author such a workflow –
1.Creating a Project in Visual Studio 2005
2.Design your Workflow
3.Code your Workflow
4.Create a feature.xml
5.Create a workflow.xml
6.Assigning a Strong-Name Key to the solution
7.Amending thePostBuildActions Batch File
8.Building the solution
9.Testing and Initiating the Workflow inhttp://mycheshireteamslocal:24001 
Step 1 – Creating a Project in Visual Studio 2005
·        Within your Virtual development image, power up Visual Studio 2005 by clicking on Start > All Programs > Microsoft Visual Studio 2005 > Microsoft Visual Studio 2005

·        Create a new project calledRollOfDiceWF in Visual Studio 2005. Base this project on the installed SharePoint Server Sequential Workflow template. New projects created with this template provide a collection of starting files to help you get started.
Step 2 – Design your Workflow
In this example we will be calculating a random number between one and six and then update the list item’s title column with that random number. 
·        Now, that you have created a project, double click workflow1.cs in the Solution Explorer pane. 
A SharePoint Sequential Workflow designer window should now be displayed and look similar to this:

In this view, Workflow programs can be created by dragging and dropping activities onto the designer screen.
Notice the onWorkflowActivated1 event that has been created automatically. This activity will fire when the workflow is activated. This needs to always be the first activity.
We could enter all of our code in the onWorkflowActivated1 event, but as a best practice we should create our code elsewhere and leave this event to activation-type activities.
In the toolbox, you will see a number of activities that you can choose from.
·        From the Toolbox, select the Code activity and drag it under the onWorkflowActivated1 event.
Your screen should now look like this:

Notice the red exclamation icon next to our Code activity. This is because we have yet to define the code which will fire when the event executes.
Step 3 – Code your workflow
·        Double-click on the Code activity in the designer window. Visual Studio will create a method for you to write code and automatically link it to the activity.
There are two important things to note here. Two private fields have been created above our code activity event method. These are theworkflowId andworkflowProperties. TheworkflowId is a uniqueguid that is assigned to a given instance of a workflow. TheworkflowProperties object is a collection of properties that relate to the workflow.
Now go ahead and modify the code to look like as below:
privatevoid codeActivity1_ExecuteCode(object sender,EventArgs e)
{
   Random rnd =newRandom() ;
   workflowProperties.Item[“”Title””] = rnd.Next(1, 6);
   workflowProperties.Item.Update();
}
This code will generate a random number between 1 and 6. TheworkflowProperties.Item is used to access the List Item that this workflow is operating from.
That’s it, the workflow is written. Now we’ll need to prepare the workflow for deployment by editing the feature.xml and workflow.xml files.
Step 4 – Creating a feature.xml file
The Visual Studio template will deploy your workflow as a feature. So the next step is to go ahead and create a feature.xml file to help facilitate the deployment.
·        Under theDeploymentFiles >FeatureFiles folder – you will find a feature.xml. Double-click this file to view its source code. This will need to be edited accordingly. To help get started insert theintellisence feature.xml code snippet into your feature.xml file. Right click over the content of your xml file and choose Insert Snippet > SharePoint Server Workflow >Feature.Xml Code as illustrated.

·        Once the feature is inserted, go ahead and modify the green place holders, so your feature.xml looks a bit like this:

 
 
   
   
   
     false
   
 

·        It is important you create your own unique GUID and populate this in your feature.xml file. New GUID’s can be created through Visual Studio by clicking Tools > Create GUID as illustrated:

Step 5 – Creating a Workflow.xml file
Similarly, after inserting the workflow.xml snippet, edit the OOTB workflow.xml to look similar to this example. This XML file is case sensitive, be very careful when editing as any errors may cause the workflow to fail following deployment:

 
 
   
   
   
     false
   
 

You will notice a number of custom input form lines of code have been removed as these are not required in this walkthrough. Left in, this will cause the workflow to fail after deployment.
·        It is important to ensure thePublicKeyToken for your assembly is added to the workflow.xml file. To get the public key value of our assembly you will need to open a Visual Studio 2005 Command Prompt. This can be found via the start menu by going Start > Program Files > Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt. In the command prompt type :sn -T “”path to your workflowdll””
Step 6 – Assigning a Strong-Name Key
Deploying the workflow again can cause a few issues. Firstly, before deploying the solution you will need to assign a strong name key to the class library by following these steps:
·        In Solution Explorer, right-click the bold project name that is in bold type, and then select Properties.
·        Click the Signing tab.
·        Select the Sign the assembly checkbox.
·        From the Choose a strong name key file: combo box, select
·        In the Create Strong Name Key dialog box, in the Key file name: text field, enter a string value.
·        ClearProtect my key file with a password, and then click OK.
·        Close the Signing window.
·        From the Build menu, click Build Solution.
·        From the File menu, click Save All.

Advertisements

One thought on “Writing a custom workflow for SharePoint 2007 with Visual Studio 2005

  1. Hiya i’m so thrilled I found your web site, I actually found you by mistake, while I was browsing Yahoo for something else, Anyway I’m here now and would just want to say thanks for the great weblog posting and a complete absorbing blog (I also adore the particular theme/design), I don’t have time to read it all right this moment although I’ve you bookmarked and in addition added your RSS feeds, so once i have time I’ll be back to go through much more

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s