Team Foundation Server – Eventing Service - Part 2 (Publishing events)

Posted by: Subodh Sohoni , on 4/16/2008, in Category VSTS & TFS (Azure DevOps)
Views: 25735
Abstract: In the first part of this article we discussed about how to subscribe to the events which are published by the Team Foundation Server (TFS). Although the events published by TFS are quite extensive, there still may exist need to subscribe to the events which are not covered by TFS. To publish such events we need to create a separate event class and an application to raise that event.
Team Foundation Server – Eventing Service - Part 2 (Publishing events)
In the first part of this article we discussed about how to subscribe to the events which are published by the Team Foundation Server (TFS). Although the events published by TFS are quite extensive, there still may exist need to subscribe to the events which are not covered by TFS. To publish such events we need to create a separate event class and an application to raise that event.
This article discusses:
-        How to publish an event which can be subscribed to
-        How to raise that event from an application with certain event related data
-        How to access the event related data
Publishing events
Events which are to be published are loosely coupled with the subscribers. The responsibility of notification of the event being raised is with the TFS. The notification service of TFS needs the .xsd file representing the event class to be present at a specific location in the file system. We begin creating that file by first creating a class library which contains the class which represents the event. This class has certain restrictions
1.       It should have a default public constructor, a constructor that does not accept any parameter.
2.       It also will have public fields which will be assigned values during raising the event.
3.       The class should be serializable i.e. should have Serializable attribute.
The example of such a class is given below:
using System;
using System.Collections.Generic;
using System.Text;
namespace WorkItemAddedEvent
    public class WorkItemAddedEventType
        public WorkItemAddedEventType()
        { }
        public string id;
        public string projectUri;
Imports System
Imports System.Collections.Generic
Imports System.Text
Namespace WorkItemAddedEvent
      <Serializable> _
      Public Class WorkItemAddedEventType
            Public Sub New()
            End Sub
            Public id As String
            Public projectUri As String
      End Class
End Namespace
Once the class is compiled into a component and a .dll is created, we can use the tool XSD.exe to convert the class to the respective .xsd file. The syntax of the command is as follows:
>XSD.exe MyComponent.dll /type:WorkItemAddedEventType /out:DirName
This command generates the schema file representing the event class. The generated schema file for the above written class would look like this:
<?xml version="1.0"encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="">
      <xs:element name="WorkItemAddedEventType" nillable="true" type="WorkItemAddedEventType" />
      <xs:complexType name="WorkItemAddedEventType">
                  <xs:element minOccurs="0" maxOccurs="1" name="id" type="xs:string" />
                  <xs:element minOccurs="0" maxOccurs="1" name="projectUri" type="xs:string" />
This generated XSD file is stored at …\Program Files\Microsoft Visual Studio 2005 Team Foundation Server\Web Services\Services\v1.0\Transforms with the same name as the class name which in this case would result in to a file named WorkItemAddedEventType.xsd.
Once the file has been stored at the mentioned location, notification service of TFS will come to know about it after IIS is restarted. Execute the >iisreset.exe command to do that. Now anyone can subscribe to this event.
Raising the published events
The event that is published can now be raised using the Team Foundation Object Model. We can create a component or a windows desktop application to raise the event. In that application we need to give the reference to following dlls:
1.       Microsoft.TeamFoundation.dll
2.       Microsoft.TeamFoundation.Client.dll
3.       The dll which contains the event class.
4.       Any additional service which we are using for example in this example since we are adding a workitem, we need to give reference to Microsoft.TeamFoundation.WorkItemTracking.Client.dll
We will need now to create an instance of IEventService and call the FireAsyncEvent method of that instance to start the notification to the subscribers. The code to do that is in the listing below:
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer("http://SEED_tfs:8080");            
IEventService EventService = (IEventService)tfs.GetService(typeof(IEventService));
WorkItemAddedEventType WIAddedEv = new WorkItemAddedEventType(); = wi.Id.ToString();
//wi is the instance of new workitem being added
WIAddedEv.projectUri = proj;      // proj is the name of the team project
The fields which are given values here can be accessed using the template as was given in the Part 1 of this article. The template which will be used to create the email may look like this:
            <title>TFS Workitem Tracking Subsystem Notification</title>
      <body style="font-family:courier new; font-size:12px;">
            <div>WorkItem has been added</div>
            <br />
                  WorkItem ID: <variable name="id" />
            <divTeam Project:=""
                  <variable name="projectUri" />
In this format the variable tag extracts the data from the event arguments and displays it.
In this article we have seen how to write your own events, how to publish them and how to raise the events which will be notified to the subscribers. This mechanism provides excellent way to extending the functionality for automation of tasks involved with TFS.
I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to my RSS Feed.

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+


Subodh is a Trainer and consultant on Azure DevOps and Scrum. He has an experience of over 33 years in team management, training, consulting, sales, production, software development and deployment. He is an engineer from Pune University and has done his post-graduation from IIT, Madras. He is a Microsoft Most Valuable Professional (MVP) - Developer Technologies (Azure DevOps), Microsoft Certified Trainer (MCT), Microsoft Certified Azure DevOps Engineer Expert, Professional Scrum Developer and Professional Scrum Master (II). He has conducted more than 300 corporate trainings on Microsoft technologies in India, USA, Malaysia, Australia, New Zealand, Singapore, UAE, Philippines and Sri Lanka. He has also completed over 50 consulting assignments - some of which included entire Azure DevOps implementation for the organizations.

He has authored more than 85 tutorials on Azure DevOps, Scrum, TFS and VS ALM which are published on is a regular speaker at Microsoft events including Partner Leadership Conclave.You can connect with him on LinkedIn .

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Dan Libre on Monday, April 21, 2008 1:19 AM
To the point content..keep it up guys!! Which version does the article target? TFS 2005 or the latest one 2008?
Comment posted by Subodh Sohoni on Tuesday, April 22, 2008 12:24 PM
Hi Dan,
Thanks for your comments. The above article target TFS 2005 although it may also work for 2008 without any change. I have not tried it on 2008 yet but may do so in a few days.