Mohammed Atef’s Technical blog

Workflow Foundation 4.0 Custom activities

WFF 4.0 helps you to implement you own custom activity. sure creating custom activity is very important part that all Workflow developers are interested in.the new Workflow Foundation 4.0 give you two ways of generating custom activities.
As we need to prove concept only, my custom activity will be simple, just I am going to build custom activity that print the message “Hello FirstName Last Name” this custom activity taking two input parameters First Name and Last Name and return the full message,let’s see how to implement this simple example by Workflow Foundation 4.0.

Using Code Activity template

Code activity template give you advantage to inherit the codeactivity class and build your custom activity from scratch but the new Workflow Foundation helps you to do that in easy way, so to implement the new custom activity follow the following steps

1- Open Visual Studio 2010 and select file->new project->workflow->workflow consol application

2- In the name textbox write CustomactivitySample as the project name

3- After the solution has opened right click in the project name and select add new item that will open the list of items as shown in the below picture

clip_image002

select code activity and enter code activity name such as “HelloMsg
4- By default you with find the below list of code has been generated in your new file that named HelloMsg.cs

   1: public sealed class HelloMsg : CodeActivity
   2:     {
   3:         // Define an activity input argument of type string
   4:         public InArgument<string> Text { get; set; }
   5:         // If your activity returns a value, derive from CodeActivity<TResult>
   6:         // and return the value from the Execute method.
   7:         protected override void Execute(CodeActivityContext context)
   8:         {
   9:             // Obtain the runtime value of the Text input argument
  10:             string text = context.GetValue(this.Text);
  11:         }
  12:     }

5- Now we are going to add our code for printing hello message

– First we will prepare the activity parameters, so replace line no 4 in the above codes list with the following list of codes

   1: public InArgument<string> FirstName { get; set; }
   2: public InArgument<string> LastName { get; set; }
   3: public OutArgument<string> HelloMessgae { get; set; }

As shown in the above codes list we have declared two input parameters first name and last name and one output parameter for returning the complete hello message.

– The last step for implementing this custom activity is to add code for using input parameters and concatenating it to return the complete hello message, so please replace line 10 at the codes list in step 4 with the following list of codes

   1: string fname = context.GetValue(this.FirstName);
   2: string lname = context.GetValue(this.LastName);
   3: string completeMsg = " Hello " + fname +" "+ lname;
   4: context.SetValue(this.HelloMessgae, completeMsg);

6- Build the project to refresh the toolbar, and you will notice new activity hellomsg activity has been added to the toolbar.

7- Open Workflow1.xaml in designer view and drag sequence activity then drag hellomsg and writeline activity by ordered in the new added sequence activity

8- Create variable named fullname and has data type string from the variables window at bottom of the workflow1 designer

9-Open the writeline property windows and set the text property to the new created variable name in the previous step

10- Open the hellomsg property window and set it is properties as follow:

a- FirstName: “Mohammed”

b- Last Name: “Atef”

c- HelloMessage: fullname which added in step no 8

Finally run the application you will see this message in the console window

“Hello Mohammed Atef”

Using Activity

Using the activity you can create your own activity and use it in another work flow as independent activity and you can pass parameters between your activity and the main workflow designer

This kind of activities is very simple as you are using the workflow designer with all toolbar activities available to implement you custom activity.

We are going to do the same business scenario in the previous example but we will add the following message before the hello message “Custom Activity using Designer.” this only to determine the message printed for each custom activity.

Now , to implement the new custom activity follow the following steps

1- Write click into the project name and select add new item then select activity icon from the windows as shown in picture below

clip_image002[4]

name that activity hellomsgActivity.xaml

2- Drag sequence activity to the new created activity named hellomsgActivity.xaml and then drag writeline activity into the sequence activity

3- Add two arguments from arguments window below the designer of that activity named hellomsgActivity.xaml

4- Write the following line into the text property of the writeline activity

“Custom Activity using Designer: Hello Mr. ” + FName + LName

5- Build the application

6- Open the main workflow1.xaml in design mode and drag hellomsgActivity activity from the toolbar

7- Open the hellomsgActivity property window and set it is properties as follows

a- FName:”Mohammed”

b-LName:”Atef”

Now Run the application you will find the following messages are printed

Hello Mohammed Atef”

Custom Activity using Designer: Hello Mr. Mohammed Atef”

Now we have finished this example and the main workflow1.xaml should be like the following picture below

clip_image002[6]

You can also download the complete example by source code from here.
Back to other Workflow Foundation 4.0  posts

I hope that helped.

Advertisements

September 26, 2010 - Posted by | .Net 2010 | , , , , ,

3 Comments »

  1. […] This custom activity receives the collection as an input argument. It expects a collection of BookStoreListItem classes. the overridden execute method first check if the list empty or not, if yes it is printing the book list is empty message, else it is loop throw the list and print each book item information, this code is very simple you can understand but if you do not know how to create custom activity please visit the following link WFF 4.0 custom activity. […]

    Pingback by Workflow Foundation 4.0 collections « Mohammed Atef’s Technical blog | September 26, 2010 | Reply

  2. Very nice post. needed just something to find “back into it” . Great!

    Comment by embeducation (@AWSOMEDEVSIGNER) | August 6, 2012 | Reply

    • Thank you for your comment

      Comment by mohammedatef83 | August 6, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: