Tuesday, April 13, 2010

Working with declarative workflows Part 1

Hi All,

We all know that we can create a great workflow using visual designer provided in visual studio as well as SharePoint designer. However there is one more way, we can create workflow. Yes the technique is called declarative workflow. Declarative workflow is all about writing XML code and then feeding this XML code to workflow runtime to execute the workflow.

Let me tell you why this can be a fun element while working in workflow. Let us say that you have defined many conditions in your workflow and you have created that workflow with Visual studio. Hence you compile that workflow and use the DLL to deploy and then use in application or in SharePoint. So you have to come back to designer, change your logic and again compile, get new DLL and deploy it to use it. This is bit overhead while working with designer. However designer workflows have their own advantages as well and there cannot be two ways on this. However we are talking about declarative workflow, so here is the advantage of using declarative workflow.

You can change your condition logic right away by just modifying the existing XML and then feeding the same to workflow runtime.

To work with XML based workflow, you only need at simples notepad and as advanced XML editor or visual studio itself.

One important part to note is whenever we work with declarative workflow; we have to use two namespaces in XML.

1) http://schemas.microsoft.com/winfx/2006/xaml
2) http://schemas.microsoft.com/winfx/2006/xaml/workflow

So let us do some particle on this.

Go ahead and create one console sequential workflow and immediately add new item and call it DeclarativeWorkflow.xml.

Open the file’s property and change its Copy to Output directory property to Copy If Newer.



And start adding following XML in that file. Moment you start typing the XMLNS attribute, it gives you an options of all namespace URIs.



Make sure that you write in proper caps. Even if you miss a single alphabet in capital letter or lower letter, you will not get an idea what is wrong with the workflow and you will receive an error while starting the workflow.

<SequentialWorkflowActivity x:Name = "Workflow1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
<SequenceActivity x:Name="SequenceActivity1">
<DelayActivity TimeoutDuration="00:00:15" x:Name="DelayActivity1"></DelayActivity>
</SequenceActivity>
</SequentialWorkflowActivity>


As you can notice, how we assign ID to the controls in ASP.Net, in the same way we have to assign the Name attribute to our activities in the XML.

Here we have defined very basic workflow with one single delay activity.

Now go ahead and add one code activity in designer surface and double click to generate handler for that and simply write down the following code in it.



And now run the application to see it in action.



Read Working with declarative workflows Part 2 for further reading.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com