Custom Activities in TFS2010

There is a good post on Jim Lamb’s blog on how to customize tfs2010 build, but I decided to blog my experience because I need some more time to make it work.

The steps to create a custom activities in tfs2010 are the following ones. First of all create an activity, like this simple TweetActivity one. Respect the example of Jim, I need to add also the BuildExtensionAttribute to make it run.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
[BuildActivity(HostEnvironmentOption.All)]
[BuildExtension(HostEnvironmentOption.Agent)]
public sealed class TweetActivity : CodeActivity
{
   [RequiredArgument]
   public InArgument<string> Username { get; set; }

   [RequiredArgument]
   public InArgument<string> Password { get; set; }

   [RequiredArgument]
   public InArgument<string> Tweet { get; set; }

   protected override void Execute(CodeActivityContext context)
   {

       TwitterService service = new TwitterService();
       String tweet = Tweet.Get(context);
       tweet = tweet.Substring(0, Math.Min(tweet.Length, 140));
       String result = service.SendMessage(Username.Get(context), Password.Get(context), tweet);
   }
}

This is a very simple activity that simply tweet a message. Then after you compile it, you need to save the assembly in a specific file of your source code, to be available from build agent.

image

You must configure the Build Controller to point at a specific folder to have it understand where to find assemblies that contains custom activities. These steps are really well described in Jim’s post, so I do not give further details.

Then you face the first problem, if you copy the default workflow of the build (to avoid changing the basic one) and open it into Visual Studio, you don’t see your custom action in toolbox so you are not able to add it to your build process. To solve this I decided to add my custom activity by hand, editing the xaml file. First of all you need to add the namespace

image

Now you need to add your custom activity in the workflow flow, I decided, for simplicity, to add it before the get latest activity, so it is the very first activity that gets executed. After all I only wants to test if my action gets executed during the build.

image

Xaml files are simple enough to edit by hand, as you can see to add the custom activity, just add a da:TweetActivity (da is the alias I used for my namespace) and configure the properties. Then I commit everything and started a build.

image

Ok it works :).

alk.

Tags: Team Foundation Server