Adding Sequential Workflow to a document library of a SharePoint site using Visual Studio 2008


Objective:

Core purpose of this article is to explain, how to attach and debug a sequential workflow using Visual studio 2008(using object model) to a document library of a SharePoint site.

In this article, I will discuss

  1. How to create a Sharepoint Site
  2. How to create Document Library inside that
  3. How to add custom properties in Document library

    Last but not least,
  4. I will discuss, how to add Sequential Workflow to document library using Visual studio 2008.

Step 1: Create SharePoint site

Follow the steps given below, to create a SharePoint site.

Step 1a: 

From site action select Create option

1.gif

Step 1b:

From Web Pages tab select Sites and Work Spaces

2.gif

Step 1c:

Give name and title of the site. And leave other setting as default. The name I am giving in C Sharp Document Center and URL is
http://adfsaccount:2222/csharpdocumentcenter/default.aspx.

3.gif

So the site will look like below,

4.gif

Step 2: Create Document Library

Step 2a:

Click on Documents from left Panel. 

5.gif

Step 2b:

Then click on Create 

6.gif

Step 2c:

From Libraries tab select Document Library.

7.gif

Step 2d:

Give a name and description.  And leave the other default settings. The name I am giving here is My Document Library.  Then Click on Create. 

8.gif

Step 2e:

Now in Document Tab you could able to see newly created Document library. 

9.gif

Step 3: Add Properties to the Document library

Click on My Document Library and then Setting.  Then select Create Column.

10.gif

I am adding three columns here,
  1. Document Status
  2. Assignee
  3. Review Comments.
Document Status Column
  1. Give type as Choice Menu to choose from
  2. Give column name Document Status
  3. Give any description
  4. Give Choice as

    Review Needed
    Review Completed
    Changes Requested

  5. Leave other fields as default and click on Ok to create the column.

11.gif 

Assignee column

Add this column exactly the way explained above. Only give type of information as Single Line of Text. Leave other fields with default values.

Review Comments column

Add this column exactly the way explained above. Only give type of information as Multiple  Line of Text. Leave other fields with default values.

Step 4: Enable Document to be edited without Checkout

Go to My Document Library then Document Library Setting.

12.gif

Then select Versioning setting from General Setting tab.

13.gif

Then scroll down and select No option for Check out. And click Ok. 

14.gif

Step 5: Create a SharePoint Sequential Workflow project in Visual Studio 2008.
  1. Start Visual Studio
  2. Open new project
  3. Select office node
  4. Select 2007 node
  5. Select SharePoint 2007 workflow from template pane
  6. Give any name. I am giving name here "CSharpWorkflow".

15.gif 

What local site you want to use for debugging?

Extra care is needed here.  Give the URL of the Site you created in above steps.

16.gif

For library select My Document Library. We created this library in step 2. 

17.gif

Leave the default setting and click on Finish to create the workflow.

18.gif

A  Sequential workflow like below will get created.

19.gif

Step 6: Create a Workflow schedule
  1. From designer select onWorkflowActivated1 and right click then Properties.
  2. In Invoke property type onWorkflowActivated and press enter.
  3. Code window will get open with event handler for Invoke properties.
  4. Again go to designer. Open toolbox and from Windows workflow version 3.0 tab select While activity. 

    20.gif
  5. Drag and drop While Activity under onWorkflowActivities. After dragging and dropping designer will look like below.

    21.gif
  6. Go to Property of While Activity and in Condition property give Code Condition.
  7. Expand the Condition property and in child condition tab type isWorkflowPending. And press Enter.  Code window will open and a method called isWorkflowPending will get added.
  8. Open the designer. Open Toolbox and select SharePoint Workflow tab.  From this select
  9. From this drag OnWorkflowItemChanged control inside while activity . After dragging designer will look like below.

    22.gif
  10. Select onWorkflowItemChanged1 and go to properties. set the properties as below, 

    23.gif

Note: Please type onWorkflowItemChanged in Invoked property.

Step 7: Handle Activity Events
  1. Define a  Global variable 

    Boolean workflowPending = true;
  2. Create a private method to check the status of the document.

    private void CheckStatus()
            {
                if ((string)workflowProperties.Item["Document Status"] == "Review Completed")
                    workflowPending = false;
            }
  3. Call CheckStatus method inside onWorkflowActivated and onWorkflowItemChanged  events.
Workflow1.cs


using
System;

using System.ComponentModel;

using System.ComponentModel.Design;

using System.Collections;

using System.Drawing;

using System.Linq;

using System.Workflow.ComponentModel.Compiler;

using System.Workflow.ComponentModel.Serialization;

using System.Workflow.ComponentModel;

using System.Workflow.ComponentModel.Design;

using System.Workflow.Runtime;

using System.Workflow.Activities;

using System.Workflow.Activities.Rules;

using Microsoft.SharePoint;

using Microsoft.SharePoint.Workflow;

using Microsoft.SharePoint.WorkflowActions;

using Microsoft.Office.Workflow.Utility;

 

namespace CSharpWorkFlow

{

    public sealed partial class Workflow1 : SequentialWorkflowActivity

    {

        Boolean workflowPending = true;

        public Workflow1()

        {

            InitializeComponent();

        }

 

        public Guid workflowId = default(System.Guid);

        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

 

        private void onWorkflowActivated(object sender, ExternalDataEventArgs e)

        {

            CheckStatus();

 

        }

 

        private void isWorkflowPending(object sender, ConditionalEventArgs e)

        {

            e.Result = workflowPending;

        }

 

        private void onWorkflowItemChanged(object sender, ExternalDataEventArgs e)

        {

 

            CheckStatus();

        }

 

        private void CheckStatus()

        {

            if ((string)workflowProperties.Item["Document Status"] == "Review Completed")

                workflowPending = false;

        }

    }

}

Step 8: Testing the Workflow

  1. Press F5 to run with debug.
  2. Site which URL you given in Step 5 will get open. In my case this is the site, I created in Step 1.
  3. If you remember, I given the name to library as My Document Library . That library is open 

    24.gif
  4. Create a New Document inside this library. Just click on New and then New Document.
  5. Press Ok to message displayed.

    25.gif
  6. If you see, all the Properties I added are attached in the document as column.   Document Status, Assignee, Review Comments are added in the document. Give some value and save the document at default location.

    Note: Please in Document Status property select Review Needed. 

    26.gif

    27.gif
  7. Close the document. Now you would able to see the workflow status is In Progress

    28.gif
  8. Click on drop down of the document and select Edit properties.

    29.gif
  9. Now change Document Status to Review Completed  and press Ok.

    30.gif
  10. Refresh the Site
Since, user has changed the status to Review Completed so now workflow status is also completed. 

31.gif

Conclusion:

In this article, I discussed
  1. How to create a Sharepoint Site
  2. How to create Document Library inside that
  3. How to add custom properties in Document library

    Last but not least,
     
  4. I discussed, how to add Sequential Workflow to document library using Visual studio 2008.
Thanks for reading

Happy coding

Up Next
    Ebook Download
    View all
    Learn
    View all