Blogs

eXpress App Framework Team

Manually starting workflows

     

In this post we are going to explore 2 different ways to start a workflow. So far we have looked at using the workflow server, together with the ObjectCreated and ObjectFitsCriteria, to automatically start the workflow. However there is often a necessity to start workflows from a client whilst entering additional parameters.

Scenario 1

An order form has been completed and the user hits enter. This will initiate a workflow that will asynchronously notify a call center employee to contact that user.

XpoStartWorkflowRequest Solution

The below code will start the workflow on the server asynchronously

var criteriaOperator = CriteriaOperator.Parse("Name = 'My workflow'");

IWorkflowDefinition definition = View.ObjectSpace.FindObject<XpoWorkflowDefinition>(criteriaOperator);

if (definition != null) {

    var request = new XpoStartWorkflowRequest(((ObjectSpace)ObjectSpace).Session);

    object currentUserId = SecuritySystem.CurrentUserId;

    request.TargetWorkflowUniqueId = definition.GetUniqueId();

    request.TargetObjectKey = currentUserId;

}

 

WorkFlowInvoker Solution

The following code will start the workflow in synch with the client

 

var criteriaOperator = CriteriaOperator.Parse("Name = 'My workflow'");

IWorkflowDefinition definition = View.ObjectSpace.FindObject<XpoWorkflowDefinition>(criteriaOperator);

if (definition != null) {

    Activity activity = ActivityXamlServices.Load(new StringReader(definition.Xaml));

    var args = new Dictionary<string, object> {{"targetObjectId", SecuritySystem.CurrentUserId}};

    IDictionary<string, object> results = WorkflowInvoker.Invoke(activity, args);

    // If needed

    //((MyObject)View.CurrentObject).WorkflowResultData = (int)results["SomeIntOutArgument"];

}

 

Scenario 2

Let’s make Scenario 1 a little bit more interesting by providing the option to choose a Product when placing the order. This time together with the current user id we need to set an additional parameter - the product id.

WCF Solution

It is interesting to note that it is also possible to apply the WorkFlowInvoker Solution from Scenario 1 here. This is because the WorkflowInvoker can receive a range of arguments. Now, in order to to model our workflow we are going to use the runtime designer. We need to create a new workflow definition, name it and set its target object to a dummy value. Since we are going to invoke the workflow using a WCF service the target object is redundant, however it is mandatory. The same rule applies to the other start workflow conditions (AutoStartWhenObjectIsCreated, AutoStartWhenObjectFitsCriteria).

image

Next we are going to use the native receive activity and bind it to the WCF service as shown.

image

it is imperative to check the CanCreateInstance attribute at this stage. At the same time we need to set the OperationName and ServiceContactName and define the following contract;

[ServiceContract]

public interface IPassOrder {

    [OperationContract(IsOneWay = true)]

    void PassOrder(Guid currentUserId, Guid productId);

}

 

Two variable declarations (productId, currentUserId) are required in order to hold them for later use. Moreover we need to assign them from the OperationName arguments.

 

 

image

To finish designing we can create a Task that will inform the employee. You can read more on this here.

image

Now, the final and most interesting part is to launch our workflow with a simple code like;

var criteriaOperator = CriteriaOperator.Parse("Name=?", "Contact customer");

var workflowDefinition = ObjectSpace.FindObject<XpoWorkflowDefinition>(criteriaOperator);

var uri = "http://localhost:46232/" + workflowDefinition.GetUniqueId();

var newEndpointAddress = new EndpointAddress(uri);

var serverWorkflow = ChannelFactory<IPassOrder>.CreateChannel(new BasicHttpBinding(), newEndpointAddress);

var productId = Guid.NewGuid();

serverWorkflow.PassOrder((Guid)SecuritySystem.CurrentUserId, productId);

 

 

 

We would appreciate your feedback on this post. Has it been useful to you? Feel free to contact us  with any further questions

Related Links
Online documentation
Blog posts

Published Jul 19 2011, 12:30 AM by Apostolis Bekiaris (DevExpress)
Filed under: , , ,
Technorati tags: 11.1 v2011.1, workflow, WF, XAF
Bookmark and Share

Comments

 

Robert Fuchs said:

Great post, thanks.

Robert

July 18, 2011 10:50 AM
 

Robert Fuchs said:

Tolis,

I would like to see an example about how to start workflows via eXpand Quartz. Would this be possible?

Thanks in advance.

July 18, 2011 10:52 AM
 

Simon de Kraa said:

Thanks Tolis! :-)

July 19, 2011 4:10 AM
 

Apostolis Bekiaris (DevExpress) said:

Thanks for your comments guys!

@Robert in this post apobekiaris.blogspot.com/.../xaf-quartz-jobscheduler-module.html you can see that IXpandScheduler exposes a XafApplication instance. This means that you can use it

to start your workflows as described here. eXpandFramework, in the future is going to use Quartz triggering logic to create activities that can be schedule.

July 19, 2011 7:17 AM
 

Robert Fuchs said:

@Tolis: Ican see that it exposes a XafApplication instance - but I don't know how to do it. That's why I asked for an example.

> eXpandFramework, in the future is going to use Quartz

> triggering logic to create activities that can be schedule

Hmm, and that means? ...

July 19, 2011 5:08 PM
 

Maxim Guz said:

Very useful, thanks.

I would like to see an example about how to design workflow with send activity.

November 16, 2011 10:46 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.