a·gen·tic a·gil·i·ty

Creating your own Event Handler

Learn how to create custom event handlers for Team Foundation Server by inheriting from AEventHandler, implementing IsValid and Run methods, and handling event data securely.

Published on
3 minute read
Image
https://nkdagility.com/resources/53h9G1A5o8G
Subscribe

Creating an event handler with the Team Foundation Server Event Handlers is very easy. You will need to inherit from the AEventHandler class which is part of RDdotNet .TeamFoundation.dll located in the install directory (I will make an SDK later).

Creating your own Event Handler

1Imports Microsoft.TeamFoundation.Client
2
3Public MustInherit Class AEventHandler(Of TEvent)
4
5    Public MustOverride Sub Run(ByVal EventHandlerItem As EventHandlerItem(Of TEvent), _                                 ByVal ServiceHost As ServiceHostItem, _                                ByVal TeamServer As TeamServerItem, _                                ByVal e As NotifyEventArgs(Of TEvent))
6    Public MustOverride Function IsValid(ByVal EventHandlerItem As EventHandlerItem(Of TEvent), _                                         ByVal ServiceHost As ServiceHostItem, _                                         ByVal TeamServer As TeamServerItem, _                                         ByVal e As NotifyEventArgs(Of TEvent)) As Boolean  End Class

Both of the methods that the AEventHandler exposes have the same signature. Hear is what it all means…

NameTypeDescription
EventHandlerItemEventHandlerItem(Of TEvent)The event handler item contains information about the handler. This includes an instance of the config data, the handler its self and a status property. The TEvent generic makes sure that everything is strongly types for the event that you are dealing with.
ServiceHostServiceHostItemBase Address of the service, Event Type, Host Config data and a link to the service host object.
TeamServerTeamServerItemConfig data for the team server as well as an instance of the Microsoft.TeamFoundation.TeamFoundationServer object
eNotifyEventArgs(Of TEvent)This object allows access to all of the event specific data including the event object, the event type, the TFS Identity object and the TFS Subscription object.

Lets look at the implementation that comes with the TFS Event Handler , the “AssignedToHandler”. The IsValid method denotes wither the handler will acrualy run at all:

1    Public Overrides Function IsValid(ByVal EventHandlerItem As EventHandlerItem(Of WorkItemChangedEvent), _                                      ByVal ServiceHost As ServiceHostItem, _                                      ByVal TeamServer As TeamServerItem, _                                      ByVal e As NotifyEventArgs(Of WorkItemChangedEvent)) As Boolean         If e.Event Is Nothing Then             Return False         End If         Dim assignedName As String = WorkItemEventQuerys.GetAssignedToName(e.Event)
2        If String.IsNullOrEmpty(assignedName) Then             Return False         Else             Return Not assignedName = WorkItemEventQuerys.GetChangedByName(e.Event)
3        End If     End Function

This method initially checks to see if the event exists and then queries the assigned name from the event using a work item event query which consists of:

1Return eventData.CoreFields.StringFields.Find(New Predicate(Of StringField)(AddressOf FindAssignedTo)).NewValue

All this does is search the String fields associated with the core work item bits to find the “System.AssignedTo” value. You could so this manually, but I have a number of queries there and you can add any number you wish.

The logic: If Event exists and assigned name is not empty then check that the assigned name is not the changed name.

So in English with all of the crap split out: Did the user assign the work item to himself? If not then send them an email!

The rest, as they say, is just logic. The “Run” method calls the “IsValid” and then sends an email if it is in fact valid, hardly rocket science.

Hopefully with this knowledge you will be able to make many many event handlers!

For the delayed CTP 1 of the TFS Event Handler I have changed the logic quite a lot but the same IsValid and Run methods exist. The parameters are, however slightly different. I have taken into account security and you will have to make your own connection to the TFS server using your own username and password. I have changed this to protect the security of the application as I want developers to be able to upload event handler assemblies and WF workflow without having to get access to the server. I ahve also changed it so the service that captures the events is not the same one that runs the handlers. This allows me to send the events between these services using MSMQ, thus giving the service some much needed redundancy.

Software Development
Subscribe

Related blog

No related videos found.

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Boxit Document Solutions Logo

Boxit Document Solutions

Genus Breeding Ltd Logo

Genus Breeding Ltd

ProgramUtvikling Logo

ProgramUtvikling

Akaditi Logo

Akaditi

Alignment Healthcare Logo

Alignment Healthcare

Trayport Logo

Trayport

Sage Logo

Sage

Emerson Process Management Logo

Emerson Process Management

Cognizant Microsoft Business Group (MBG) Logo

Cognizant Microsoft Business Group (MBG)

Xceptor - Process and Data Automation Logo

Xceptor - Process and Data Automation

Schlumberger Logo

Schlumberger

Healthgrades Logo

Healthgrades

NIT A/S

ALS Life Sciences Logo

ALS Life Sciences

Bistech Logo

Bistech

Graham & Brown Logo

Graham & Brown

Boeing Logo

Boeing

Brandes Investment Partners L.P. Logo

Brandes Investment Partners L.P.

Nottingham County Council Logo

Nottingham County Council

New Hampshire Supreme Court Logo

New Hampshire Supreme Court

Washington Department of Enterprise Services Logo

Washington Department of Enterprise Services

Royal Air Force Logo

Royal Air Force

Ghana Police Service Logo

Ghana Police Service

Department of Work and Pensions (UK) Logo

Department of Work and Pensions (UK)

ProgramUtvikling Logo

ProgramUtvikling

Freadom Logo

Freadom

Workday Logo

Workday

Qualco Logo

Qualco

Teleplan Logo

Teleplan

Illumina Logo

Illumina