1
votes

I'm trying to write some code that monitors the TFS WorkItems on my local workstation but at the moment I'm having problems getting the events to fire.

I have subscribed to FieldChange event of WorkItem but it doesn't fire when I change/update any workitem.

The code below is a console application that I think should work,bu it doesn't . Does anyone know how to successfully subscribe to these events?

Any help in this matter is appreciable .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.Client;
using System.Net;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.Framework.Client;

namespace TFSEvents
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Uri serverUri = new Uri(@"http://tfs");
                string username, password;
                Console.WriteLine("Enter Username:");
                username = Console.ReadLine();
                Console.WriteLine("Enter password:");
                password = ReadPassword();
                NetworkCredential cred = new NetworkCredential(username, password);
                TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(serverUri, cred);
                tfs.EnsureAuthenticated();
                IEventService vs = tfs.GetService<IEventService>();
                var wiww = tfs.GetService<WorkItemStore>();
                var wi = wiww.GetWorkItem(4671);
                wi.FieldChanged += new WorkItemFieldChangeEventHandler(changeHandler);
                var x = vs.GetAllEventSubscriptions().ToList();
                Console.WriteLine("Press \'q\' to quit.");
                while (Console.ReadLine() != "q") ;
            }
            catch (Exception e)
            {

            }
        }

        private static void changeHandler(object o, WorkItemEventArgs e)
        {
            Console.WriteLine(e.Field);
        }
   }
}
1

1 Answers

0
votes

You cant handle WorkItem events in that way, it will only work for a single instance. There are only a fixed set of events you can handel:

http://nkdagility.com/tfs-event-handler-for-team-foundation-server-2010/

And you need an eventhandler to use them:

http://nkdagility.com/tfs-event-handler-in-net-3-5-part-2-handling-team-foundation-server-events/

UPDATE

If you don't want to use BisSubscribe.exe you can create a subscription using the API. If you check my bad code on http://tfseventhandler.codeplex.com/SourceControl/latest#RELEASE/v1.3/RDdotNet.TeamFoundation/Managers/TeamServerManager.vb you should see a "RegisterEvent()" method that will do the job.