Purpose of this post is to show you how to get started developing SharePoint MOSS 12 workflows with Visual Studio 2008 by creating a simple sequential workflow. Let's say we want to create a workflow that creates a task each time user creates new announcement in Announcement list.
First step is to launch Visual Studio 2008 and navigate to File → New → Project (or press Control + Shift + N) to open the New Project dialog. One note on launching Visual Studio 2008 – you have to launch it with administrator privileges in order to create SharePoint Workflow projects. SharePoint workflow project templates are located under Office 2007 node.
As soon as you click OK the New Office SharePoint Workflow wizard is displayed. On the first page of workflow wizard you can choose the name of your workflow and the local URL (you can use http://localhost) to which you want to deploy your workflow. For this example accept the defaults on first page and click the Next button.
First wizard page
On the next wizard page you can choose a library or list to associate your workflow with. The other two combo boxes are for selecting workflow history list and task list – by default there's only one history list and one task list on your SharePoint site (if you have added additional task lists, you will see them here).
Second wizard page
Selecting the "Automatically associate workflow" checkbox will associate the workflow with the selected list. If you unselect the check box you have to take care of workflow association manually.
Since we said we want our workflow to be associated with Announcements list, go ahead and select Announcements list from the first combo box and click Next. Last wizard page appears and it looks like the figure below.
Third (last) wizard page
There are three more settings on the last wizard page you can select. Below is the description of settings:
- Manually by users: when checked, users can manually start the workflow.
- When an item is created: when checked, the workflow is started when a list/library item is created (or uploaded).
- When an item is changed: if checked, the workflow is started each time item in the list is changed.
We won't worry about these settings in our sample so click Finish to close the wizard. Once you click Finish the wizard is closed and our workflow project is created. I won't go into the details on files that are created in the project at this time. Let's focus on creating a task when a new announcement is added to the Announcements list.
Workflow designer should already be open and it contains one default activity: onWorkflowActivated1. This is executed, well, when workflow is activated. Go ahead and follow the below steps to add another activity to the designer:
- Open the toolbox (View → Toolbox or Control + Alt + X) if it's not opened.
- Locate CreateTask activity (it should be in the SharePoint Workflow tab) and drag & drop it onto designer, below the onWorkflowActivated1 activity. Sequential workflow designer should look like the one in figure below.
Notice the red exclamation mark on the createTask1 activity which is telling us that we have to set some additional properties. To do this, right click on createTask1 and from context menu and select Properties (or click on the activity and press F4 to open Properties window). When properties window is opened you can notice small exclamation mark next to the CorrelationToken property. Correlation token is basically an identifier – you can read more about CorrelationToken here (http://msdn.microsoft.com/en-us/library/ms475438.aspx). Type 'taskToken' in the CorrelationToken property. Notice the exclamation mark is still there. Expand the property (notice the + sign before the property name) and for OwnerActivityName select Workflow1.
In order to create a task we have to specify the TaskId and TaskProperties properties:
- Locate TaskId property in the property grid.
- Click on the ellipsis (...) next to the property.
- Binding dialog is displayed.
- Click 'Bind to a new member ' tab.
- In 'New member name' text box type: 'taskId'.
- Select Create Field.
- Click OK.
Above steps will create a field in our code which represents task ID. You can repeat the same steps for TaskProperties – name the field taskProperties. Now that we have our ID and properties only thing left is to set the values for our task:
- Double click createTask1 activity.
Once the code window is displayed type in the following code:
private void createTask1_MethodInvoking(object sender, EventArgs e)
taskId = Guid.NewGuid();
taskProperties.AssignedTo = "firstname.lastname@example.org";
taskProperties.Description = "Description of my task created with workflow.";
taskProperties.DueDate = DateTime.Today.AddDays(5);
taskProperties.Title = "My super task from super cool workflow.";
The above code assigns new GUID to task ID and assigns some values to the task. You probably noticed that you don't have to actually call a method to save or create the task – the createTask activity takes care of this. All you need to do is to set properties for the task e.g. description, title, due date, assigned to. There are several additional properties you can set – just type taskProperties and after you type the dot ('.'), intellisense shows up and you can browse through other properties.
Now we're ready to debug! Insert a breakpoint in the createTask1_MethodInvoking and the breakpoint will be hit. When you press F5 your workflow is deployed and the browser should open up at Announcements list. To test the workflow, add a new Announcement to the list. Once you add the new announcement there should be a new column added to the list. The name of that column should be equal the name of our workflow (SimpleWorkflow) and it contains the status of the workflow (you should see Completed in that column).
If you click on the status (e.g. Completed) you are redirected to the workflow status page and you will see the task that was created as a part of our workflow. It should look similar to the one in the figure below.