RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Wrangling SharePoint Workflows with Visual Studio : Page 4

SharePoint and Workflow may be the most powerful combination since chocolate and peanut butter, but the trick is harnessing their combined power. That isn't as easy as it first seems, but in this article you'll learn how create a SharePoint workflow in Visual Studio from start to finish.


Associating the Workflow to a Document Library
Now that the workflow and associated content types are created it's time to associate those content types and the workflow to a set of lists in SharePoint. For these instructions you'll need a blank team site. I've created one called Grades under the root of my server. You may use the root site of your installation or create a site underneath the root site to associate the content types and workflow.

To associate the content type for the grading document, which has the score and notes field, to the document library and to remove the default content type, follow these steps:

  1. Open up the document library and then from the Settings menu select the Document Library Settings entry.
  2. The first step is to activate the content type. Do that by clicking Advanced Settings in the General Settings section. The top item on the Document Library Advanced Settings page is Allow management of content types. Select the Yes radio button and scroll down to the bottom of the page and click the OK button.
  3. Back on the Customize Shared Documents page in the Content Types section, click the 'Add from existing site content types.'
  4. On the Add Content Types page (which is in the drop-down list for 'Select site content types from'), select Grading. Click the Add button to move the GradingDocument to the box of added content types on the right. Click the OK button to finish.
  5. The next step is to remove the Document type so that all documents that are created in the document library will be the GradingDocument type. Click on the Document link under the Content Types heading.
  6. From the List Content Type page click the 'Delete this content type' link.
  7. Click the OK button on the popup dialog that asks if you're sure.

With the content type in place it's time to associate the workflow. Follow these steps:

  1. From the Customize Shared Documents page in the Permissions and Management column click the Workflow Settings link.
  2. On the Add a Workflow page in the 'Select a workflow template:' listbox, scroll down and select the Grades workflow template.
  3. Enter a name for this workflow association: "Grades" works fine.
  4. Scroll down to the Start Options section and check the box for 'Start this workflow when a new item is created.' Click the OK button. The Grades workflow is added to the document library.

All of the plumbing is done. All that is left is to test the workflow and ensure that it works.

Testing the Workflow
Now that the workflow is associated with a document library and set to activate on new items all that needs to happen is for a document to be uploaded. When this happens the Grades workflow kicks off and adds a new task to the workflow. Figure 10shows the task created when the feature.xml file from the Grades project was uploaded to the Shared Documents library.

Figure 10. A new task is created by the Grades Workflow when a document is uploaded.
Figure 11. The Grades workflow is completed.
Figure 12. The results of the Grades workflow is shown.

If you update the task to include a score and notes you'll notice that the task doesn't always go away immediately. Sometimes when you first refresh the task list the task will appear and sometimes it won't. Because the workflow and the refresh of the task list page occur at the same time it's possible you'll see or not see the task depending upon which thread executed quicker. Refreshing the task list page will definitely make the task disappear.

Switching back to the Shared Documents list you'll see that there is a column called Grades—which matches the association name you provided when you associated the workflow with the list—and that it has a value of Completed. Figure 11shows the completed Grades workflow.

The properties of the document were in fact updated based on what was entered in the task. Figure 12show that the student got a score of 100 and a note of "Good Work!"

SharePoint Workflows don't have to be scary. Yes, they are powerful. Yes, they are flexible. Yes, they require a lot of knowledge. However, using SharePoint Workflows can quickly create solutions to difficult problems.

Author's Note: This article is based on Chapter 10 of the forthcoming SharePoint MVPs book. The SharePoint MVP book is the collaborative effort of 17 SharePoint MVPs.

Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date