Creating Custom Workflow Activity – Windows Workflow Foundation 4.0

n
n

nIt’s been long since I written new blog post , but today gotnsome time to write one on which I was working recently.

n

nThis time it’s little away from SharePoint world, just tonstroll Windows Workflow Foundation 4.0. The time I am writing this post,nBollywood music is running over my ears so I am really in mood of writing J

n

n

n

nWorkflow Foundation

n

nMicrosoft has provided us a very strong foundation sincenrelease of .NET Framework 3.5 known as Windows Workflow Foundation (WF), and itnis getting much better with every release. The latest one is WF 4.0 and isnreally useful in mapping your business processes to workflows. The basic ideanbehind any workflow creation is to automate the business processes and do itnmore declarative way.

n

n

n

nNeed of Custom Activities

n

nThough the framework provides you lots of inbuilt activitiesnso that you can grab them and directly use in your workflow application as it is,nbut again in most of the cases business scenarios are complex and requirementncannot be solved directly using out of the box activities. So in such cases whatnneeds to be done? Nothing worry, similar to SharePoint workflows – windowsnworkflow foundation also supports creating custom activities. (Awkward to saynbut WF seems much much stronger when compared to SharePoint workflow engine). Youncan also provide input parameters to your activity and activity can return younoutput which can be later used in the workflow.

n

n

n

nExample

n

nOk enough of theory and let’s get in to the action.nn

n

nIntentionally I have taken very simple example for demo, itnsimply uses a custom activity and workflow simply adds a new product in thenproducts database and finishes. Nothing much but just to understand thencreation and execution of custom activity.

n

nI will start by creating a project in Visual Studio 2012

n

nOpen up visual studio and click on create new project. Fromnleft side of new project window – click on workflow category and selectnWorkflow Console Application project.

n

nYou will see a Workflow.xamlnopened up in the editor. This is basically playground for you where you cannstart designing you workflow.

n

nIf you take a look at leftnside of the window , you will see something familiar as Toolbox (remembernASP.NET web applications? ) and here is the place where you will find all outnof the box activities provided by WF 4.0

n

nI have designed a very simplenworkflow which prints a line to the windows when workflow starts and do somenexecution of activities and then again prints line to window when workflownfinishes.

n

n

n

n

n

n

n

n

n

nNow wait a minute, did you observe something called asnAddProduct there in the editor as well as in the toolbox? From where on thenearth that came?

n

nNothing suspicious going on here so don’t worry, this is thencustom activity which I created which simply adds a new product in the productsninventory. I won’t go in much details of the how I have created the databasenand used it using the entity framework but we will concentrate more on creatingnthat custom activity and passing parameters to it.

n

n

n

nImplementation

n

nCustom activities in the windows workflow foundation arennothing but the class which is derived from a class known as ‘CodeActivity’.nn

n

nOnce you create your own class and derive it fromnCodeActivity , you will need to override the execute method of the parentnclass. This method gives you the workflow execution context so that you cannplay around input and output parameters.

n

nFollowing code is just for understanding purpose on how wencan create custom activity, you can add any complex logic in the executenmethod.

n

nSo here is the AddProduct activity code

n

n

n

n

n

n
n

npublic class AddProduct : CodeActivity

n

n{

n

n  public InArgument<int> ProductId { get; set; }

n

n  public InArgument<string> ProductName { get; set; }

n

n  public InArgument<string> ProductDescription { get; set; }

n

n  public InArgument<int> ProductPrice { get; set; }

n

n  public InArgument<int> ProductDiscount { get; set; }

n

n  public OutArgument<string> Result { get; set; }

n

n

n

n  protected override void Execute(CodeActivityContext context)

n

n  {

n

n    try

n

n    {

n

n      using (var productEntities = new ProductsEntities())

n

n      {

n

n        productEntities.ProductInfoes.Add(new ProductInfo

n

n        {

n

n           ProductId = ProductId.Get(context),

n

n           ProductName =nProductName.Get(context),

n

n           ProductDescription =nProductDescription.Get(context),

n

n           ProductPrice = ProductPrice.Get(context),

n

n           ProductDiscount =nProductDiscount.Get(context)

n

n        n});

n

n         productEntities.SaveChanges();

n

n        }

n

n              

n

n    context.SetValue(Result, “Product with Id : “ + context.GetValue(ProductId) + ” Added Successfully!!”);

n

n     }

n

n     catch (Exception ex)

n

n     {

n

n       context.SetValue(Result, “Error While Adding Product : “ + ex.Message);

n

n     }

n

n

n

n   }

n  }
n

n

nAs you can see that this activity is having 5 inputnparameters and one output parameter which is later consumed by workflow writenline activity (at end of workflow) to display the message to users.

n

nOnce you are creating the class and done with implementationnof execute method, then just rebuild your solution and observe the toolbox.nFramework automatically adds the custom activity for you in the toolbox.

n

nNow question is how to pass parameters to the activity?

n

nOnce the activity is available in the toolbox, simply dragnand drop to the workflow. Right click on activity and go to the propertiesnwindow.

n

nHere you will need to provide the input parameters and alsonif you are returning something from your custom activity then you will need toncatch that output to some workflow variable.

n

n

n

n

n

n

n

n

n

nI am passing some hardcoded values as parameters to thenactivity but this can be made dynamic by passing in workflow arguments.

n

nResult is nothing but the output message which our activitynis producing from execute method. I have simply assigned that result to thenworkflow variable called as varResult , so that I will be able to use it innworkflow context.

n

nThat’s all guys, I hope now you know how to create customnactivities in Windows Workflow Foundation and use them to make your lifeneasier. Hope this helps someone.

n

Leave a Comment