TFS Event Handlers

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).
Imports Microsoft.TeamFoundation.Client

Public MustInherit Class AEventHandler(Of TEvent)

    Public MustOverride Sub Run(ByVal EventHandlerItem As EventHandlerItem(Of TEvent), _
                                               ByVal ServiceHost As ServiceHostItem, _
                                               ByVal TeamServer As TeamServerItem, _
                                               ByVal e As NotifyEventArgs(Of TEvent))

    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...

Name Type Description
EventHandlerItem EventHandlerItem(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.
ServiceHost ServiceHostItem Base Address of the service, Event Type, Host Config data and a link to the service host object.
TeamServer TeamServerItem Config data for the team server as well as an instance of the Microsoft.TeamFoundation.TeamFoundationServer object

e NotifyEventArgs(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:
    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)
        If String.IsNullOrEmpty(assignedName) Then
            Return False
        Else
            Return Not assignedName = WorkItemEventQuerys.GetChangedByName(e.Event)
        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:
Return 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.


Related Releases Related Pages

Last edited May 30, 2008 at 8:22 AM by hinshelmw, version 3

Comments

No comments yet.