Create TFS Event Subscription programmatically with TFS API

Event Subscriptions in TFS are a very powerful mechanism to get notification about event happening in TFS. You can use the Alerts Explorer, that comes with the TFS Powertools (like I describe in this post), but sometimes it is easier to create subscriptions automatically.

In this post I will show you how to use the TFS API to list subscriptions and to create a subscription.

First create a project which contain references to the following assemblies:

  • Microsoft.TeamFoundation.WorkItemTracking.Client.dll
  • Microsoft.TeamFoundation.Client.dll
  • Microsoft.TeamFoundation.dll

These can be found in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Then create a TfsTeamProjectCollection object that contains a reference to your Team Foundation Server. Of course you can also use your own credentials but sometimes it is handier to use a central account which is used for all subscriptions.

private TfsTeamProjectCollection tfs;

private string TfsAdminuserName { get; }

private System.Security.SecureString TfsAdminuserPassword { get; }

private string TfsAdminuserDomain { get; }

private string TfsServerURI { get; }

 

public void CreateAdminServer()

{

    NetworkCredential nc = new NetworkCredential(

        TfsAdminuserName, 

        TfsAdminuserPassword, 

        TfsAdminuserDomain

        );

    tfs = CreateTFSInstance(nc);

}

 

private TfsTeamProjectCollection CreateTFSInstance(NetworkCredential nc)

{

    if (tfs == null || (tfs != null && tfs.Credentials != nc))

    {

        try

        {

            //URI = http://tfsserver:8080/tfs/DefaultCollection

            tfs = new TfsTeamProjectCollection(new Uri(TfsServerURI), nc); 

            tfs.Authenticate();

            tfs.EnsureAuthenticated();

        }

        catch (WebException webEx)

        {

            throw new ApplicationException(webEx.Message, webEx);

        }

    }

    return tfs;

}

To list all subscriptions (for all users) use :

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

List<Subscription> ls = es.GetAllEventSubscriptions().ToList();

To create a subscription use:

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

string filter = string.Format("\"PortfolioProject\" = '{0}'",projectName);

 

            

DeliveryPreference del  = new DeliveryPreference();

del.Address = "Webservice address";

del.Schedule = DeliverySchedule.Immediate;

del.Type = DeliveryType.Soap;

            

string eventName = string.Format("<PT N=\"Display name of event \"/>");

 

es.SubscribeEvent("WorkItemChangedEvent",filter,del,eventName);

The SubscribeEvent method has a parameter “classification”. This is amongst others used for the display name of the TFS event. Use the xml format like  <PT N=\"Display name of event \"/> to make sure the events gets displayed with a name instead of (Unnamed Event)

image

The Event Types you can use are amongst others:

  • BuildCompletionEvent
  • BuildStatusChangedEvent
  • CheckinEvent
  • WorkItemChanged

Check the alerts explorer to view your alerts

image

Hope this helps!

About these ads

3 Responses to “Create TFS Event Subscription programmatically with TFS API”

  1. Thanks for the comment you left on my blog. I only just saw it now. The code above was very helpful in creating a UI for TFS Event Subscription management.

  2. Are you using also the server object model for this code or is the client object model sufficient ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 67 other followers

%d bloggers like this: