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

Annunci

TFS Integration – TF237124 – Work Item is not ready to save

With this post, I want start a new category: TFS Integration…

For a project I had to create some TFS Work Items programmatically using the TFS API. However saving the work item resulted in the following error message:

TF237124: Work Item is not ready to save

This error message doesn’t give any useful information. What’s important to know is that you can configure a lot of rules before a work item is valid. It’s important to validate the WorkItem prior to save. The validate() method will return an arraylist of invalid fields.

This will give you all the information you need to know why this workitem cannot be saved (In this case I have 3 invalid fields; in the following image, for example, U have the “Backlog Priority” field with an invalid value).

validate