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)
The Event Types you can use are amongst others:
- BuildCompletionEvent
- BuildStatusChangedEvent
- CheckinEvent
- WorkItemChanged
Check the alerts explorer to view your alerts
Hope this helps!
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.
Are you using also the server object model for this code or is the client object model sufficient ?
When team explorer is installed this should sufficient