To most SharePoint people, this announcement is nothing new. Many have proclaimed the glory which is SharePoint workflow. And in fact, there are plenty of samples of SharePoint workflow solutions within the SharePoint internet community and also within the SharePoint SDK itself. But after presenting SharePoint workflow at a local technology conference, something was apparent in the kind of questions that were raised by the audience. Although there were many examples of SharePoint workflows floating around (I generated a few myself), there weren't as many great step-by-step, detailed walkthroughs of building Workflows in SharePoint starting from the basics. This post is the first attempt of bridging that gap, explaining how an experience .NET developer who is experienced with Windows Workflow can host their workflows in a SharePoint environment; step by step.
Let's start with some assumptions. I assume:
And next let's set some expectations of what you will get from reading this article:
My intent is to keep this walkthrough at a very introductory level, only looking at the very basics needed to build, deploy, and execute a workflow within SharePoint. Future articles will build onto this example, adding more useful functionality and introducing more complex subjects like custom association forms, initiation forms, InfoPath Services, content type association, etc. So let's get started...
The first thing you're going to need is a basic development environment. I recommend using Virtual PC 2007 for local desktop development because it allows you to run the necessary SharePoint server infrastructure (Windows Server 2003, AD, SQL, etc) safely from your desktop. Plus, if you use Virtual PC, you can use the publicly available Virtual PC virtual hard drives that come pre-installed with all the MOSS prerequisites - Windows Server, AD, SQL, .NET 2.0 & 3.0, and SharePoint 2007. You can get this prebuilt image here:
http://www.microsoft.com/downloads/details.aspx?FamilyID=67f93dcb-ada8-4db5-a47b-df17e14b2c74&DisplayLang=en
My walkthrough from this point forward assumes you're using this image - if you're not you'll need to translate my instructions to the environment that you are using. In addition to the base MOSS image, you will also want to install:
http://www.microsoft.com/downloads/details.aspx?familyid=6D94E307-67D9-41AC-B2D6-0074D6286FA9&displaylang=en
Once you have all of the prerequisites installed, you can proceed onto the next steps...
Step 1: Start a new Workflow Project within Visual Studio
I know some of you are already wondering why I immediately reach for Visual Studio vs. SharePoint designer. I don't intend on going into great detail into this matter, but in short here is my reasoning:
Ok, so now that we decided on Visual Studio, let's create a new SharePoint Server Sequential Workflow project:
You should now have a new solution with a new workflow project. Notice that you get a few additional default folders and files that normally aren't included with the default Windows Workflow project
One thing to note is the parameter "NODEPLOY" at the end of the call to PostBuildActions.bat. By default the parameter is set to NODEPLOY which means you can build the project but the project output will not be deployed to the local SharePoint server. If you change this parameter to "DEPLOY" the batch file will attempt to deactivate, uninstall, deploy, re-install, and activate your feature on the local SharePoint server.
Step 2: Complete the feature.xml file
If you don't see a "SharePoint Server Workflow" folder, see Appendix A to learn how to configure the SharePoint Server Workflow snippets using Visual Studio's Snippet Manager.
Your feature.xml file should now look like this:
The feature.xml code snippet inserts the basic xml markup needed to define a SharePoint feature:
Type "This is the title of my feature"
Type "This is my feature description"
Leave the default "12.0.0.0".
Leave the default value
Leave the default value.
Delete this element for this demo.
Delete this node for this demo.
When you are finished, your feature.xml file should look like this:
Step 4. Sign your workflow assembly
Your workflow assembly will be installed in the GAC, so it must be strongly signed. To sign your assembly use Visual Studio
Step 5: Complete Workflow.xml
Type "My First Workflow"
Type "My Workflow Description"
Use GuidGen to create a new Guid for this attribute.
If you left the defaults in previous steps, this should be "SharePointWorkflowLibrary1.Workflow1"
Delete this attribute
This is out of scope for this example so delete this element
When you are finished you should end up with a workflow file that looks something like this:
Step 6: Add some logic to your workflow
Once you have configured the feature.xml and workflow.xml files, it time to get to the real work of add logic to your workflow. In this example we are not going to do much at all in our workflow; the purpose of this post is to illustrate how to get a simple workflow project started and deployed to a SharePoint site.
Your workflow should now look like this:
You can either hardcode a message by type a string into the HistoryDescription field or you can provide a dynamic message at runtime by binding this property to either a property or field in the workflow code behind. A nice feature of Windows Workflow is that it is really simple to bind workflow activities to properties of the workflow:
Step 7: Set the project post build to deploy the workflow
Once you have a configured feature and workflow, and the workflow compiles successfully, it's time to test it by deploying it to the local SharePoint instance.
What this will do is execute the PostBuildEvent.bat file found in the project. Since we have left all the defaults, we shouldn't have to modify the batch file - but in most cases you will have to modify this file to get it to work against your own development environment. The exact detail of what the batch file is doing is beyond the scope of this post, but in short the batch file:
Once these steps complete, the workflow is ready to test in the local SharePoint environment
Step 8: Test the feature in SharePoint
To test your new feature you need perform a few actions in SharePoint. First, you will need to associate the workflow to a list or library. This allows instances of the workflow to be executed against items in the list. Next you need to instantiate the workflow by executing the workflow against an item in the list or library. This creates a new instance of the associated workflow and executes it. Finally, you will want to view the workflow history log to verify your workflow actually executed successfully.
Associate the workflow
Conclusion
Hopefully everyone now can create new workflow project and deploy them to their test environments for test execution. In out example our workflow really didn't do anything useful, but it did demonstrate how to access the item the workflow is executing against so more complex logic could be implemented. In future articles, we will create more complex workflows that really exercise the powerful features of SharePoint workflow
Appendix A
How to configure SharePoint Workflow snippets in Visual Studio 2005
If you are reading this appendix, then I assume Step 2 did go as planned. No fear, adding the SharePoint workflow code snippets is just a few clicks away: