Creating a simple custom TFS 2010 build activity

Creating a custom build activity for TFS 2010 is fairly simple, in this post I’ll show you how to create a simple check out activity.

To create a custom build activity you need to inherit from either CodeActivity or CodeActivity<T>, both of which reside in System.Activities. CodeActivity<T> is used to return a value out of the activity where T is the return type.

To get data into your class you need to create your properties using InArgument<T> (InOutArgument<T> and OutArgument<T> are also available). For example, if you need to get a file name into your class you would create a property: public InArgument<string> Filename { get; set; } which will appear as a string property named Filename in the workflow designer.

Use the attribute [RequiredArgument] to specify that a value is required for a property.

Finally you need to override the Execute method, this is the method that will contain the execution code for your build activity. The code activity context will be passed into this method which will allow you to access your properties. Once you have dragged the activity onto your build workflow you simply need to set the Workspace property to Workspace and set the FilePath property to the server path for the file you want to check out.


public sealed class CheckOutFile : CodeActivity
{
  [RequiredArgument]
  public InArgument<Workspace> Workspace { get; set; }

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

  protected override void Execute(CodeActivityContext context)
  {
    // Obtain the runtime values
    Workspace workspace = context.GetValue(Workspace);
    String filePath = context.GetValue(FilePath);

    // Get the pending changes to the filePath
    PendingChange[] pendingChanges =
    workspace.GetPendingChanges(filePath, RecursionType.Full);

    // If no pending change for the filePath was found
    // it means the file is can be checked out
    if (pendingChanges.Length == 0)
    {
      // Check out the file
      workspace.PendEdit(filePath, RecursionType.Full);
    }
  }
}