Share via


Subscribe to Team Foundation Server (TFS) event WorkItemChangedEvent

VSTS provides the event model. Event Service allows subscription of events using SMTP or SOAP protocol. Sample code below provides a web service that subscribes to WorkItemChangedEvent. Web service logs the Event XML generated by triggering WorkItemChangedEvent to Windows Event Log.

[WebMethod]

public void Notify(string eventXml)

{

    try

    {

        string strSource = "Sample Events"; //name of the source

        string strLogType = "Application"; //type of the log

        string strMachine = "."; //machine name

       

        if (!EventLog.SourceExists(strSource, strMachine))

        {

            EventSourceCreationData eventObj = new EventSourceCreationData(strSource, strLogType);

            EventLog.CreateEventSource(eventObj);

        }

   EventLog eLog = new EventLog(strLogType, strMachine, strSource);

        eLog.WriteEntry(eventXml);

    }

    catch (Exception ex)

    {

        throw new Exception(ex.Message);

    }

}

       

This web service needs to be subscribed by the TFS. Subscription can be done either by using bissubscribe.exe or by custom code. Below is code snippet to provide event subscription

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

using Microsoft.TeamFoundation.Client;

using Microsoft.TeamFoundation;

using Microsoft.TeamFoundation.Server;

namespace EventSubscription

{

    class Program

    {

        static void Main(string[] args)

        {

            string userName // domain user

            string deliveryAddress // Web Service that needs to subscribe. Above code

            string tfsServer; // TFS Server Name

            int subscriptionId; // Subscription id

            // establish connection with TFS server

            TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(tfsServer);

            //SubscribeWorkItemChangedEvent(ref tfs, ref userName, ref deliveryAddress);

               

            UnSubscribeWorkItemChangedEvent(ref tfs, subscriptionId);

  }

        private static void SubscribeWorkItemChangedEvent(ref TeamFoundationServer tfs, ref string userName, ref string deliveryAddress)

        {

            IEventService eventService = tfs.GetService(typeof(IEventService)) as IEventService;

  DeliveryPreference delivery = new DeliveryPreference();

            delivery.Type = DeliveryType.Soap;

            delivery.Schedule = DeliverySchedule.Immediate;

            delivery.Address = deliveryAddress;

eventService.SubscribeEvent(userName, "WorkItemChangedEvent", "", delivery);

        }

        private static void UnSubscribeWorkItemChangedEvent(ref TeamFoundationServer tfs, int subscriptionId)

        {

            IEventService eventService = tfs.GetService(typeof(IEventService)) as IEventService;

            eventService.UnsubscribeEvent(subscriptionId);

        }

    }

}

- Events can be subscribed either by running the bissubscribe.exe or the console application written above.

- Once events are subscribed you can verify the subscription by looking into dbo.tbl_subscription table under tfsintegration DB in data tier of TFS.

- Upon saving the work items in Team explorer, VSTS event service tiggers the WorkItemChangedEvent event, subscribed web service called the Notify method which will log the event XML in the window log of Web Service hosted machine

Comments