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,

  1. I will discuss, how to add Sequential Workflow to document library using Visual studio 2008.

Step1: Create SharePoint site

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

Step 1a:

From site action select Create option


Step 1b:

From Web Pages tab select Sites and Work Spaces


Step1c:

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.


So the site will look like below,


Step 2: Create Document Library

Step 2a:

Click on Documents from left Panel.

 


 

Step 2b:

Then click on Create

 


 

Step 2c:

From Libraries tab select Document Library.


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.


Step 2e:

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


Step 3: Add Properties to the Document library

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


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.

     


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.


Then select Versioning setting from General Setting tab.


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


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”.

     


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.


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


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


A Sequential workflow like below will get created.


Step 5: 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.

     


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

     


  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.

     



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

     


    Note: Please type onWorkflowItemChanged in Invoked property.

 

Step6: 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;

}

}

}

Step7: 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

     


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

     


  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.



  7. Close the document.

     

Now you would able to see the workflow status is In Progress


  1. Click on drop down of the document and select Edit properties.

     


  2. Now change Document Status to Review Completed and press Ok.

     


  3. Refresh the Site

     

Since, user has changed the status to Review Completed so now workflow status is also completed.

 


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,

  1. I discussed, how to add Sequential Workflow to document library using Visual studio 2008.

Thanks for reading

Happy coding

4 responses to “Adding Sequential Workflow to a document library of a SharePoint site using Visual Studio 2008.”

  1. Thank u. good article

  2. Thank u Dhananjay Kumar,

    good article.

    Keep posting new articles.

  3. awesome article. Good to start with the Sequential workFlows

  4. Thanks, for writing on workflow . hope will continue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com