Difference between workflow created using SharePoint Designer and Visual Studio Designer for Windows Workflow Foundation.

SharePoint Designer

Visual Studio 2005 Designer for Windows Workflow Foundation.

 

Can write only sequential workflows.

Can write both sequential and state machine workflows.

 

Automatic deployment against the specific list or library against which workflow is being designed.

Can be deployed as a feature.

Logic is defined declaratively using Steps which comprises of Conditions and Actions

Logic could be defined through custom code written using C# or VB.NET.

Workflows could be associated to a specific list or library.

Workflow can be authored as Template which once deployed could be associated with any list or library.

Workflow modifications not possible.

Workflow modifications are possible using Modification forms built using ASP.NET or InfoPath form.

Workflow markup, rules all are stored as a document library on the site.

Workflows are compiled as an .NET assembly.

Can’t be debugged.

Debugging is possible using Visual Studio.

Using Workflow Object Model in SharePoint.

I was assigned a task to create a simple aspx page where the user could see all the all the different documents, workflows running against them, workflows task information as well as workflow history.

Here we can make use of SPWorkflow and SPWorkflowTask class.

The page would be displaying the information in the following manner

Workflow status for following document :-SampleDocument1
Workflow name :- Approval Workflow
Workflow Task Title
First Team Task
Second Team Task
Third Team Task
Workflow History Description
The approval workflow has started waiting for and Second Team to respond
Task has been created and assigned to First and Second Team
First and Second team has completed their task

Workflow status for following document :- SampleDocument2
Workflow name :- Approval Workflow
Workflow Task Title
First Team Task
Workflow History Description
The approval workflow has started waiting for First and Second Team to respond

The sample code for getting the above information

protected void Page_Load(object sender, EventArgs e)

{

//SPWorkflowManager myWFMgr = new SPWorkflowManager();

SPSite objSite = new SPSite(http://servername:port”);

SPWeb objWeb = objSite.OpenWeb();

SPList myList = objWeb.Lists[“ListName”];

// for each document within the Library

foreach (SPListItem myListItem in myList.Items)

{

Response.Write(“<b>Workflow status for following document :-</b>” + myListItem[“Title”].ToString());

Response.Write(“</br>”);

// Get the workflows associated

foreach (SPWorkflow myWF in myListItem.Workflows)

{

// Get the name of the workflow

Response.Write(“Workflow name :- “+ myWF.ParentAssociation.Name);

Response.Write(“</br>”);

Response.Write(“<b>Workflow Task Title </b>”);

Response.Write(“</br>”);

// for each workflow running get the workflow tasks and history information

foreach (SPWorkflowTask myWFTask in myWF.Tasks)

{

Response.Write(myWFTask[“Title”].ToString());

Response.Write(“</br>”);

}

// for each workflow running get the history information

Response.Write(“<b>Workflow History Description</b> “);

Response.Write(“</br>”);

SPList myList1 = objWeb.Lists[“Workflow History”];

SPQuery query = new SPQuery();

query.Query = “<OrderBy><FieldRef Name=”ID”/></OrderBy>” +

“<Where><Eq><FieldRef Name=”WorkflowInstance”/>” +

“<Value Type=”Text”>{“ + myWF.InstanceId.ToString() + “}</Value>” +

“</Eq></Where>”;

SPListItemCollection historyListItems = myList1.GetItems(query);

foreach (SPListItem i in historyListItems)

{

Response.Write( i[“Description”].ToString());

Response.Write(“</br>”);

}

}

Response.Write(“</br>”);

}

}

That’s it …

Publishing workflow as an ASP.NET Web Service.

 

The workflows developed using windows workflow foundation can be published as a web service. To publish a workflow as a web service, we are provided with two activities. WebServiceInput and WebServiceOutput activity.

 

To create a workflow to be published as a web service do the following

 

Create a new Empty Workflow Project.

 

Add a new interface class to the project which would be used by our WebServiceInput activity.

 

Suppose this is our interface class.

 

interface IAddInterface

                {

        int AddNumber(int firstNumber, int secondNumber);

                }

 

Now right click on the project and add a new Sequential Workflow.

 

Now drag and drop three activities. First WebServiceInput, Code and WebServiceOutput.

 

For webServiceInputActivity1 specify following properties.

 

InterfaceType –IAddInterface.

MethodName AddNumber

 

As soon as method is added we’ll see two new properties added over there one for each parameter specified in the interface method.

i.e. firstNumber and secondNumber.

 

Create two variables to bind with them and one more variable to hold the result.

 

  public int intFirstNumber;

  public int intSecondNumber;

  public int total;

 

 

Specify intFirstNumber, intSecondNumber for firstNumber and secondNumber property of webServiceInputActivity1.

 

Finally set IsActivating as true;

 

 

Now right click code activity and select generate handlers to create a handler method and specify the following code for it

 

private void codeActivity1_ExecuteCode(object sender, EventArgs e)

        {

            total = intFirstNumber + intSecondNumber;

        }

 

Now select webServiceOutputActivity1 and set its properties as following

 

InputActivityName = webServiceInputActivity1

 

ReturnValue = Activity=AddWebServiceWorkflow, Path=total (i.e. to total variable)

 

That’s it now we want to publish it as workflow.

 

For this right click the project and Select Publish as a web servcie option.

 

 

This would create a new webservice which would be named as worklflowProjectName.worfklowMethodName_WebService.asmx.

 

Now we could use it just like any other web service created using ASP.NET.

 

WebServiceInputActivity recieves input from the client using the webservice. It can be  associated with single web service method defined inside the interface. The WebServiceInputActivity can be followed by the set of activities which could be built in or custom activity. If the response has to sent back to the calling client than we need to make use of WebServiceOutputActivity.

Passing Parameters to Workflow from Host Application `

To pass parameter to Workflow.

First we need to create a local variable and property in the workflow class.

private string _message = string.Empty;

public string Message

{

get { return _message; }

set { _message = value; }

}

From our hosting application to pass the value for the parameter,

we need to create a generic Dictionary object which uses String for key and Object as value for the parameter.

Key for parameter should exactly match the public property in the workflow.

Dictionary<String, object> wfParam = new Dictionary<string, object>();

wfParam.Add(“Message”, “My Hello World”);

Now we need to pass this Dictionary object to the workflow using one of the overloaded method for CreateWorkflow

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1),wfParam );

Bye…

Understanding a Hello World program in Windows Workflow Foundation

Hi,

1. Create a new Sequential Workflow Console Application project.

2. Open Workflow1.cs in design mode.

3. Drag a Code activity to the workflow at (Drop activities to create a sequential workflow)

4. Select Code activity and in the properties window for ExecuteCode property write MyMessage and the press enter twice, which will create a method with the same name.

private void MyMessage(object sender, EventArgs e)
{
Console.Write(“Hello World”);
Console.ReadLine();
}

5. Press F5 to run the project.

This completes our first program in windows workflow foundation.

What we have done over here is that

We have created a console application which is hosting a workflow. Workflow requires an application as a host. It could be a console application, windows forms application,

asp.net application and even a Windows service.

We can found the code used for hosting the workflow in Program.cs.

Let’s try to understand the code

// First we are creating an instance of WorkflowRuntime

// WorkflowRuntime – is responsible for starting our workflow, firing events for different situations

// while the task is executing.

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())

{

// Our application and the workflow, both are running on different threads.

// We need our application to wait long enough before our workflow either gets completed or terminated(due to error).

// For this AutoResetEvent is used.

// Here an instance of AutoResetEvent is created i.e. waitHandle

// calling waitHandle.WaitOne() will make the main thread to wait until it is signalled using

// waitHandle.Set(), which will be done when workflow either gets complete or gets terminated.

AutoResetEvent waitHandle = new AutoResetEvent(false);

 

// Here anonymous method is used to declare an event handler for WorkflowCompleted event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set()

workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)

{waitHandle.Set();};

 

// Here anonymous method is used to declare an event handler for WorkflowTerminated event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set(), which releases the console application from its wait

workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)

{

Console.WriteLine(e.Exception.Message);

waitHandle.Set();

};

 

// Workflow instance is composed of one or more activities. WorkflowRuntime executes these workflow instances.

// workflowRuntime.CreateWorkflow->Creates a workflow instance and if the workflow runtime hasn’t been started

// the CreateWorkflow method calls StartRuntime method of WorkflowRuntime

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1));

 

//Starts the execution of the worflow instance

instance.Start();

 

// The main thread waits till it is signalled to proceed further i.e. waitHandle.Set()

waitHandle.WaitOne();

}

Bye…