TFS Event Handler for Team Foundation Server

Yesterday I needed to implement create something to update our internal application when a task on TFS is changed. After some time on scouting I found this excellent post about a new type of integration available in TFS, namely server side event handlers, that is executed within the TFS context.

In the past if you wanted to execute something whene events occurs in TFS you needed to implement a WCF service with a predefined signature and subscribed to the event using bissubscribe.exe.Now this can be done using server side events.

Before to start to development we need to install the TFS server on our dev machine because we need the follow libraries:

using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Server;

We can find these inside …Microsoft Team Foundation Server 11.0\Application Tier\Web Services\bin folder. We can install the basic setup.
Now we can run Visual Studio as admin and start to create our Event Handler project.
We create a standard C# class library.

using System;
using System.Diagnostics;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.Framework.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Server;

namespace Algraps.TestProjects.TFSServerHandler
{
    public class WorkItemChangedEventHandler : ISubscriber 
    {
        public Type[] SubscribedTypes()
        {
            return new Type[1]{typeof(WorkItemChangedEvent)};
        }

        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)
                {
                    WorkItemChangedEvent ev = notificationEventArgs as WorkItemChangedEvent;
                    int workItemId = Convert.ToInt32(ev.CoreFields.IntegerFields[0].NewValue); 
                    EventLog.WriteEntry("WorkItemChangedEventHandler", "WorkItem " + ev.WorkItemTitle + " - Id " + workItemId.ToString() + " was modified");
                }

            }
            catch (Exception)
            {
            }
            return EventNotificationStatus.ActionPermitted;
        }

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

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

In the code every time a work item is modified on TFS, we write the Title and the id on Event log.

The Deploy is simple: Open the project properties and go to the Build tab. Modify the Output Path by browsing to the Plugins directory on TFS. This will result in a new
deployment of your event handler every time you build. The deployment of a server side event handler couldn’t be simpler, just drop the assembly containing the event handlers into the Plugins folder of TFS, which is located at ..Microsoft Team Foundation Server 2010\Application Tier\Web Services\bin\Plugins.

We can debug this handler. To do this one, we need to attach to Process w3wp process that hosts TFS. This should cause your event handler to be executed immediately.

title

In the WorkItemChangedEvent object, we have ev.CoreFields.IntegerFields and ev.CoreFields.StringFields These two array all all properties of our workitem. For every workItem we have two attributes: NewValue and OldValue.

In NewValue we have the value modified and in the OldValue there is the value before to modify.

imgnewvalue

We discussed on 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 list

2 thoughts on “TFS Event Handler for Team Foundation Server

  1. Is it possible to subscribe on BuildCompletionNotificationEvent and change event properties in method ProcessEvent? I need to update Build Description before notification is sent to email but don’t know how. Thanks

  2. Hi Alessandro,
    I know its a old article, any help would be appreciated, I have followed your article and registered my plug-in, but the debugger doesn’t hit the breakpoints at all, also if i try to build my project again, the DLL is getting locked even if i stop the w3wp, i am having to restart my machine to release the dll. Any idea what is going wrong?

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...