In this short article I would like to demonstrate how to use the Visual Studio 2010 SDK to retrieve the Build History for a specific Team Project and Build Definition and retrieve the Change Sets and Work Items associated to the build. Before getting started we need to install the Visual Studio 2010 SDK.
The namespaces used in this sample are included in the following assemblies:
- Microsoft.TeamFoundationServer.Client
- Microsoft.TeamFoundationServer.Build.Client -
- Microsoft.TeamFoundationServer.VersionControl.Client
- Microsoft.TeamFoundationServer.WorkItemTracking.Client
using System;
using System.Diagnostics;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace BuildHistory
{ class Program
{ static void Main(string[] args)
{ Trace.Listeners.Add(new ConsoleTraceListener());
var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("[TFS SERVER URL"), new UICredentialsProvider()); tfs.EnsureAuthenticated();
var buildServer = tfs.GetService<IBuildServer>();
var versionControl = tfs.GetService<VersionControlServer>();
var buildDetails = buildServer.QueryBuilds("[TEAM PROJECT NAME]", "[BUILD DEFINITION NAME]"); foreach (var build in buildDetails)
{ Trace.WriteLine("Build: " + build.BuildNumber);
var changesets = InformationNodeConverters.GetAssociatedChangesets(build);
if (changesets == null)
continue;
foreach (var summary in changesets)
{ Trace.WriteLine("Changeset Summary: " + summary.ChangesetId + " " + summary.Comment);
var changesetItem = versionControl.GetChangeset(summary.ChangesetId);
foreach (var file in changesetItem.Changes)
Trace.WriteLine("Affected file: " + file.Item.ServerItem);
foreach (var workItem in changesetItem.WorkItems)
Trace.WriteLine("Work Item: " + workItem.Id + " " + workItem.Title); }
Trace.WriteLine(string.Empty);
}
Console.ReadLine();
}
}
}
I hope you found this useful