How to Create Custom Workflows in Windows SharePoint Services 3.0

SharePoint is an incredibly powerful platform when it comes to managing flows of data, and it seems odd that more people don't understand or use that functionality. Charles Lee builds on his explanation of lists in SharePoint for beginners and gives you a clear, straightforward demonstration of how to build a custom workflow to automate some of your more mundane everyday workflow tasks.

In this second article in my series on Windows SharePoint Services 3.0 , I am going to show you how to add a simple custom workflow . My favourite definition of workflow is nice and simple.

A process and/or procedure in which tasks are completed.

The majority of workflows involve a person in the process at some point.  SharePoint is ideal for this. There are other types, such as ‘system’ workflows which pass messages between disparate systems, and there are specific tools to manage these, such as Microsoft BizTalk Server.

If you have purchased the enterprise SharePoint product Microsoft Office SharePoint Server (MOSS) 2007 then you will have seen that there are several workflows included , such as ‘Approval’, ‘Collect Feedback’, ‘Collect Signatures’ .  However, at some point you are going to have to create and customise your own workflow.  Fortunately, Microsoft has now provided you with Microsoft Office SharePoint Designer, which is a free product and makes this task simple and productive. To follow through the examples in this article you will need to download a copy of SharePoint Designer from Microsoft’s website.

You can customise SharePoint sites with SharePoint Designer. This is a tool for designing and developing custom workflows that then allows you to and attach these to your lists and libraries.  This is done with a very simple wizard-like interface. Once you understand the concepts of creating workflow in this manner you will be surprised by what can be achieved without the need to turn to custom code and Visual Studio.

The easiest way of introducing workflow in SharePoint is to show you a simple example that makes small calculations and changes.  This is slightly artificial because SharePoint also provides us with EventReceivers which are much better for small changes based upon an add/edit/delete within a list. The example used in this article would actually lend itself to an EventReceiver rather than a workflow, but serves to show the principles. To get things up and running quickly, I am going to add a custom workflow to the example begun in the first article in this series.

Planning a workflow

 We are going to develop a simple workflow to manage the stock of our products and to alert a stock controller that we need more inventory when it runs short.  In order to do this I have added a new column to the Products list called Inventory and set it to 500 for all our products.

831-Sharepoint_workflow_clip_image002.jp

Before we can begin designing this workflow, we must plan out exactly what it will do.  The easiest way to do this is to draw out a flowchart-type diagram representing the process.  Now you could go to town on this and use Microsoft Visio or some other drawing tool, but a simple hand drawn sketch is just as good, as long as you have an accurate idea of what your workflow needs to do.  Below is a sketch of our workflow (please excuse my handwriting).

831-Sharepoint_workflow_clip_image004.jp

Once you have planned out your process then you need to design this in SharePoint Designer.

Creating a basic workflow

We need to attach our workflow to the Orders list so that the inventory is updated every time a new order is made.

To create a new custom workflow:

  • Open SharePoint Designer.
  • Select File > Open Site
  • In the Site name: text box enter the full URL to your site.
  • Click Open
  • Select File > New > Workflow

At this point SharePoint Designer needs to retrieve some information from your site so this may take a few seconds.

You are now presented with the workflow designer. This is the main interface that you will use to create and modify your workflows. It is designed to work in stages.

  • In the workflow designer type a name in the box labelled ‘Give a name to this workflow:’
  • Select the Orders list from the drop down menu labelled ‘What SharePoint list should this workflow be attached to?’
  • Within the options labelled ‘Select workflow start options for items in Orders:’ ensure that the checkbox labelled ‘Automatically start this workflow when a new item is created’ is checked, but none of the others.

831-Sharepoint_workflow_clip_image006.jp

  • Click Next

You are now in a position to create a series of steps for this workflow.  Each step consists of a number of lists of actions which can be executed depending upon a condition. Conditions can be selected from the list shown below.  If none are specified then the actions listed will be executed regardless.

831-Sharepoint_workflow_clip_image008.jp

In your example you simply want to update the Inventory column for every new order so there is no condition to meet.  However this is a powerful area of workflow and its worth exploring some of these different conditions.

  • In the ‘Step Name’ box enter a name that indicates what this step will do.  I have used ‘Update Inventory’
  • Under Actions select ‘More Actions…’
  • You should now see the workflow actions dialog.  This will allow you to choose from a range of possible actions to perform within this workflow.  If you select All Actions from the Select a Category drop down menu then you should see all available workflow actions.

831-Sharepoint_workflow_clip_image010.jp

  • As per our workflow diagram we need to get the quantity field from our order into a variable.  This is done using the ‘Set Workflow Variable’ action.  So select this from the Choose an Action list, and click Add.
  • You will notice that our action is represented by a sentence within the actions of this workflow step.  This sentence contains links which enable you to configure the variables assigned to the action.

831-Sharepoint_workflow_clip_image012.jp

  • Click on ‘workflow variable’ and select Create a new variable.

831-Sharepoint_workflow_clip_image014.jp

  • In the Name box type ‘OrderQuantity’ and select Number from the Type drop down menu.
  • Click OK
  • In the actions list select the value link within our action.

831-Sharepoint_workflow_clip_image016.jp

  • Now select the fx button in order to lookup a value.

831-Sharepoint_workflow_clip_image018.jp

  • Leave Source set to Current Item
  • Select Quantity in the Field drop down menu.
  • You need to perform a calculation based on this, so click on Actions and select ‘Do Calculation’ you may need to select the More Actions option to find this action.
  • There are three properties to set on this action so click the first ‘value’ and select the fx button.
  • You need to look up the inventory of the purchased product from the Products list, so select Product in the Source drop down menu.

831-Sharepoint_workflow_clip_image020.jp

  • Select Inventory in the Field drop down menu.

We have two additional options in this dialog now to allow us to identify the item in the products list that we want to get this value for.  This type of operation is common within workflows in SharePoint so you should take some time to understand what is happening here.

Under ‘Find the List Item’ you need to choose a field and value which uniquely identify the product in question.  Ideally this would be something like the ID column.  However we do not have a method to retrieve the ID of the list item we are interested in, we only have the lookup column on our existing item and that contains the Product Name.   However the product name should also be unique so we will use this.

  • In the Field drop down menu under ‘Find the List Item’ select Products:ProductName

831-Sharepoint_workflow_clip_image022.jp

  • Next to the Value box select the fx button to perform a lookup.
  • Leave Source as Current Item, but in the Field drop down menu select Product
  • Click OK to close the Define Workflow Lookup dialog.
  • You will get a notification from SharePoint Designer.  This is telling you that your lookup is not guaranteed to return a unique value (because you could enter the same Product Name twice); however in our situation this is acceptable so you can click Yes to continue.

831-Sharepoint_workflow_clip_image024.jp

  • Click on the ‘plus’ within the action description and select minus.

831-Sharepoint_workflow_clip_image026.jp

  • Click on the remaining ‘value’ within the action description and select the fx button.
  • In the Source drop down menu on the Define Workflow Lookup dialog select Workflow Data.
  • In the Field drop down menu select Variable: OrderQuantity

831-Sharepoint_workflow_clip_image028.jp

We now have a variable called Calc which contains the updated inventory count for our product.  So the remaining part of our workflow is to update the Products list with the new inventory count.

  • Under the actions drop down menu select Update List Item.
  • Click on ‘this list’ in the action description.
  • In the Update List Item dialog select Products from the List drop down menu.

831-Sharepoint_workflow_clip_image030.jp

  • Click on Add to select a field on this list to update.
  • Select Inventory under Set this field
  • Click on the fx button to lookup the value.
  • In the Source drop down menu on the Define Workflow Lookup dialog select Workflow Data.
  • In the Field drop down menu select Variable: calc
  • Click OK

831-Sharepoint_workflow_clip_image032.jp

  • Click OK in the Value Assignment dialog
  • You need to use exactly the same settings as you did previously to identify the list item within the Products list.

831-Sharepoint_workflow_clip_image034.jp

  • Click on OK and click Yes on the warning dialog.
  • Finally click Finish on the Workflow Designer dialog.

This workflow is now complete and SharePoint Designer will have attached this to the Orders list for you.  If you now go and create an Order for a new Product you will see that the Inventory field on your associated Product list item is updated accordingly.

The purpose of this simple workflow has been to introduce you to the power within workflows and how they can quickly and easily simplify some of the more mundane tasks within your business processes.  Now try creating a new workflow to ensure that the Total field for your order is calculated for you based on the Price field of the Products list.  This uses the same skills you will have learned from this article and it will help to ‘cement’ this technique in your memory.

Next time I will be looking at how developers can create custom workflows within Visual Studio 2008 and harness the full power of Windows Workflow Foundation.

1555-ANTS_60x60_Logo_CP.gif How does your SharePoint application perform? The Beta release of ANTS Performance Profiler 8 adds support for SharePoint 2013, helping rapidly identify and understand SharePoint performance issues. Try the Beta.