Create a Team Foundation Server event handler

Reading time ~1 minute

Sometimes you need to do special stuff when a work item, build, etc changes in your Team Foundation Server. The best option that you have is to create a server event handler. To do that you need to follow this steps:

1.- Create a new Class Library project

[caption id="attachment_92" align="alignnone" width="955"]Create Class Library Create Class Library[/caption]

2.- Add reference to those libraries

[caption id="attachment_93" align="alignnone" width="447"]libraries libraries[/caption]

You could add the first three ones from Assemblies -> Extensions in the add reference window:

[caption id="attachment_94" align="alignnone" width="790"]add reference add reference[/caption]

You have to add the other two libraries from C:\Program Files\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin

3.- Create a class that implements the ISubscriber interface

public class WorkItemChangedEventHandler : ISubscriber

4.- Implement the interface. To do that you have to implement the SubscribedTypes method:

public Type[] SubscribedTypes()
{
    return new Type[1] { typeof(WorkItemChangedEvent) };
}

The name property

public string Name
{
    get { return "WorkItemChangedEventHandler"; }
}

The priority property

public SubscriberPriority Priority
{
    get { return SubscriberPriority.Normal; }
}

And the ProcessEvent method

public EventNotificationStatus ProcessEvent(TeamFoundationRequestContext requestContext, NotificationType notificationType,
                                                    object notificationEventArgs, out int statusCode, out string statusMessage,
                                                    out ExceptionPropertyCollection properties)
{
    statusCode = 0;
    properties = null;
    statusMessage = String.Empty;
    try
    {
        if (notificationType == NotificationType.Notification && notificationEventArgs is WorkItemChangedEvent)
        {
            var ev = notificationEventArgs as WorkItemChangedEvent;
            // Do what you want
        }
    }
    catch (Exception exception)
    {
        TeamFoundationApplicationCore.LogException("Error processing event", exception);
    }
    return EventNotificationStatus.ActionPermitted;
}

In this example we are only subscribing to WorkItemChangedEvent. But you can subscribe to another events. To do that, just add more event types in SubscribedTypes method. You could find a list of available events here.

Can I debug the library?

Yes you can, and it's easy. First of all, change the output path in the project settings window and put C:\Program Files\Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin\Plugins\.

[caption id="attachment_95" align="alignnone" width="809"]project properties project properties[/caption]

And after that, attach your Visual Studio 2012 to the w3wp.exe process.

Conclusion

In this article we have seen how to develop a Team Foundation Server event handler and how to debug it. A very usefull technique in some scenarios.

See you soon!

ANTLR and JavaScript

In the last few weeks I've been working on Crystal Gazer, a nodejs console application to gather information from your Git repository. Yo...… Continue reading

Calling a Step Function

Published on July 09, 2017