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
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
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
You can also download the complete example by source code from here.
Back to other Workflow Foundation 4.0 posts
I hope that helped.
1 Comment »
Leave a Reply
-
Archives
- May 2011 (2)
- January 2011 (2)
- December 2010 (1)
- September 2010 (8)
- August 2010 (1)
- March 2010 (1)
- November 2009 (1)
- October 2009 (3)
- September 2009 (2)
- August 2009 (1)
- July 2009 (7)
- June 2009 (10)
-
Categories
-
RSS
Entries RSS
Comments RSS


[...] 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. [...]