How to create a workflow programmatically?

Are there any samples that would show how to create and/or modify a workflow programmatically, i.e. from a program, not from VS.NET IDE or a hosted designer? For example, the program would create a new workflow, add a couple of activities to it, and save the serialized workflow definition (in a file or database). I can find examples explaining how to modify a workflow instance at run time, but I need to create a workflow definition, not the instance. Any hints? Thanks.

Alek

[481 byte] By [AlekDavis] at [2008-2-23]
# 1

The Order Processing State Machine example on the WindowsWorkflow.net site shows how to dynamiclly update a workflow instance to add a state activity. The sample is available here. When running the OrderApplication, just right-click on an workflow instance in the list and select the item from the context menu to dynamically add activities.

James Conard
Architect Evangelist - Windows Workflow Foundation
http://www.WindowsWorkflow.net
http://blogs.msdn.com/jamescon

JamesConard at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 2

James,

You say that the sample "shows how to dynamiclly update a workflow instance", but this is not what I need; I need to create/modify a workflow definition, not the instance of a running workflow.

Alek

AlekDavis at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 3
If you want end user to create workflows, you can host the designer; see this thread :
Designer and the Toolbox

YvesLorphelin at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 4
Using the designer assumes using an ActiveX control in the browser (assuming Web app). This is a valid -- and probably preferred -- approach for some scenarions, but unfortunately we cannot require this (what if a customer runs Linux?). So, considering not using the designer, there must be a way to define a workflow programmatically (and generate XOML definition, which we can then store in a database), at least this is what we were told at the TAP event, but I cannot find any samples or information how to do this. Actually, we have several teams looking for this feature, so we're a little stuck at this point.
AlekDavis at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 5
Alek,

The tutorials in the SDK are written so that you don't actually use the designer at all. The sequential workflow, state machine workflow and custom activity tutorials are all code based tutorials.

As far as serializing the workflow once it's running, take a look at my blog entry here: http://blogs.msdn.com/markhsch/archive/2005/09/28/475033.aspx

Let me know if you need more help in this area.

Mark Schmidt
Programmer Writer
Windows Workflow Foundation
Microsoft

MarkSchmidt-MSFT at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 6
Mark,

Thanks for the feedback. I checked the Simple Sequential Workflow sample, and if other samples use the same approach, this is not quite what I'm looking for. The sample contains the already programmed custom workflow class, and I am looking for a way to "create" a custom workflow class on the fly. Maybe I am not expressing myself correctly, but for some reason all the responses address the questions I did not ask. Just out of curiosity (so that I understand what I am saying wrong), why do you point to the explanation ot the serialization of the running workflow? I thought I mentioned several times that I do not care about changing workflow instances, I need to change the workflow definitions. Am I not using the right terminology? Should I phrase the question differently?

Regards,

Alek

AlekDavis at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 7
Let me take a crack at it... you want to programmatically define the workflow.. you want to use code to define the workflow (not markup).

Clearly the designer does this. Below is the code for that would define a very simple workflow (no activities added etc).

<using statments omitted>

namespace WorkflowConsoleApplication1

{

public sealed partial class Workflow1: SequentialWorkflow

{
public Workflow1()
{

this.ID = "Workflow1";

}

}

}

I'm confused as to what you mean by "on the fly" though? I think you want your custom application to generate the appropriate code which would create the workflow as it was being constructed. Is that correct?

DennisPilarinos at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 8
Alek,
This post from Mark Schmidt might be of help somehow ?
YvesLorphelin at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 9
For what it's worth, I understand exactly what you want - or at least I think so :-)

I've been thinking about the same thing, and my guess is that the way to do it would be through creating or altering XAML files (pure code can also be generated and compiled programatically, of course). But, this is only guessing, for the moment. Anyone from MS care to butt in here?

Being able to programatically create or alter workflow definitions enables the creation of really dynamic systems, and is in my opinion maybe the most exiting part of the whole picture...

Arne

arnej at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 10

Alek Davis:

It sounds like you want to build your own workflow authoring tool that doesn't use the Visual Workflow Designer control. Here's a code example that shows how to programmatically define a new Sequential Workflow with a single Delay activity and then saves it to a .xoml file. Then you can compile the .xoml file using the WorkflowCompiler class. Dharma Shukla posted an example of using the WorkflowCompiler class on his blog here.



// Create a new Sequentail Workflow
SequentialWorkflow
wf = new SequentialWorkflow();
wf.ID =
"SequentialWorkflow1";

// Create a new Delay activity
Delay delay1 = new Delay();
delay1.ID =
"delay1";
delay1.TimeoutDuration =
new TimeSpan(0, 0, 20);

// Add the Delay activity to the workflow
wf.Activities.Add(delay1);

// Serialize the workflow to an XML file using the WorkflowMarkupSerializer
System.IO.StreamWriter writer = new System.IO.StreamWriter(@"c:\temp\sequentialworkflow1.xml");

WorkflowMarkupSerializer serializer = new WorkflowMarkupSerializer();
serializer.Serialize(wf, writer);
writer.Close();




I hope that helps.
James Conard
Architect Evangelist - Windows Workflow Foundation
http://www.WindowsWorkflow.net
http://blogs.msdn.com/jamescon

JamesConard at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 11
Dennis, no this is not what I am asking. By "on the fly", I mean that my program must be able to create and modify workflow definitions (not instances) at run time without using the designer. We'll build our own GUI allowing end users to create workflows, add/remove/update activities, set attributes, etc, so the program must be able to do whatever the designer does to generate workflow definitions.

Mark, Yves, thanks, the BLOG entry offers some hints.

Arne, thank you! I knew I was not crazy. :-)

James, yes, this is exactly what I was trying to say. Basically, we -- and by "we" I mean several teams working on projects with similar functionality -- are building our own workflow authoring tools, which do not use the designer. The sample you provided is helpful (although, my colleague who tried to use it on a test project encountered an error when attempting to compile a XOML-based workflow: "Attribute 'Class' not found on the root activity," but I'm not sure if this is the framework problem; will keep looking), but it is not enough. We need to know how to define activity document hierarchy (not sure if this is the right term): add/remove/reorder activities. For example, if I have two sequential activities, how do I specify which one goes first, and which one is second? If there is a conditional activity with two branches (the total of three activities), how do I specify which branch is called from which condition (again, excuse me if I do not use the right terminology)? Say, I have a workflow with three sequential activities, and I want to remove the one in the middle, swap the order of execution of the remaining two, and append a new one at the end. Which API's should I use for this? And again, I'm talking about changing the workflow definition, not the instance. Unfortunately, I cannot seem to find any references/samples explaining how to approach this; all samples that I saw deal with dynamic updates to workflow instances, not the workflow definitions. I'm pretty sure that besides our teams, there will be others who would need to do what we're doing, so some samples and general suggestions -- like which classes/methods to use when authoring workflows programmatically, etc -- would be really helpful. Thanks.

AlekDavis at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 12
Hi Alek,

There indeed is a way to achieve what you are trying to do. You can create a workflow programmatically by using the workflow object model (which is the code that the VS designer generates in the .Designer.cs file in the background).

You can then save the workflow to a file as markup, and even compile the workflow programmatically. Our hands on labs(http://msdn.microsoft.com/workflow), specifically Lab 12 has an example of how to serialize a workflow as markup to a file. Check out the loader.cs file found in the completed lab. Here's a snippet showing the use of the markup serializer we provide:

StreamWriter writer = new StreamWriter(workflowMarkupFile);
try
{
WorkflowMarkupSerializer workflowMarkupSerializer = new WorkflowMarkupSerializer();
workflowMarkupSerializer.Serialize(workflow, writer);
}
finally
{
writer.Close();
}

Hope this helps!
Angel Azcarraga

AngelAzcarraga at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 13
Angel, the serialization part seems easy. Once I have a workflow definition in memory, serializing -- and then deserializing -- it seems straightforward, but how do I build the workflow definition using the object model? What I'm looking for is the description and/or samples of the workflow object model (thank you for mentioning a new term). Although several chapters of the SDK help file mention the workflow object model, there is no explanation how to use it, i.e. which classes/methods can be used to define an activity hierarchy in the workflow object model.
AlekDavis at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...
# 14
Hi Alek,

I think I can clarify several things for you. So anytime you create an activity or a workflow through code, as in one of the examples above:

// Create a new Sequentail Workflow
SequentialWorkflow
wf = new
SequentialWorkflow();
wf.ID =
"SequentialWorkflow1";

// Create a new Delay activity
Delay delay1 = new Delay();
delay1.ID = "delay1";
delay1.TimeoutDuration = new TimeSpan(0, 0, 20);

// Add the Delay activity to the workflow
wf.Activities.Add(delay1);

you're using the object model. If you're wondering how you specify order (left to right, up and down, etc.), it's basically like any other DOM (e.g. XML DOM). If you had children a,b,c to the children collection of an activity in the order b,c,a, then this is the order in which they will execute. The same case for activities with branches, etc. You are basically defining the structure with the order in which you add objects, and the parent/child relationships you give them.

If you want to see a good example of what the OM code looks like for a complex workflow, open the VS designer and create a workflow of your choice. Then open the .Designer.cs file and check out the code generated inside the InitializeComponent() method. This is precisely the code you want to create to define your workflow.

Please let me know if you need further details on this.

Thanks!
Angel

AngelAzcarraga at 2007-9-9 > top of Msdn Tech,Software Development for Windows Vista,Windows Workflow Foundation...

Software Development for Windows Vista

Site Classified