Introduction
Visual Studio 2010 Team Foundation Server (TFS 2010) is an enterprise-level product for managing software projects throughout the entire application lifecycle. A major requirement of TFS is source-code control, which enables developers to store code in a central repository. Source control implies version control, which keeps track of every change that has been checked in by developers. Version control allows many developers to work on the same project and the project's source-code files with a reduced risk of lost code or overwriting other developers' changes. Because source control is a core need of developers, often IT departments initially bring TFS into their organization for its source-control capabilities, sometimes we have requirement to access the TFS check in and source control details from our C# code. This tip explain how to access TFS source control check in details and changeset details.
Using the Code
Create a new Windows application using Visual Studio. Create a UI as you need like the given screenshot. Add the following DLL assemblies from the .NET tab of Add references.
Microsoft.TeamFoundation.dll
Microsoft.TeamFoundation.Build.Common.dll
Microsoft.TeamFoundation.Client.dll
Microsoft.TeamFoundation.Common.dll
Microsoft.TeamFoundation.VersionControl.Client.dll
Add the following namespaces:
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
In Form Load event:
string TFSAddr;
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Text = "";
dateTimePicker1.Value = DateTime.Now.Add(new TimeSpan(-30, 0, 0, 0));
dateTimePicker2.Value = DateTime.Now;
}
Create a ChangeDateFormat
method for date format conversion:
private static VersionSpec ChangeDateFormat(DateTime date)
{
string Gvndate = string.Format("D{0:yyy}-{0:MM}-{0:dd}T{0:HH}:{0:mm}", date);
return VersionSpec.ParseSingleSpec(Gvndate, "");
}
Add button click event to the form:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "";
string TFSAddr = " http://testTFSServer:8080/tfs/TestApplicationProject ";
textBox2.Text = TFSAddr;
textBox2.ReadOnly = true;
TfsTeamProjectCollection TFSProjectCollection =
TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(TFSAddr));
var versionControl = TFSProjectCollection.GetService<versioncontrolserver />();
VersionSpec versionFrom = ChangeDateFormat(dateTimePicker1.Value);
VersionSpec versionTo = ChangeDateFormat(dateTimePicker2.Value);
IEnumerable results = versionControl.QueryHistory
("$/", VersionSpec.Latest, 0, RecursionType.Full, textBox1.Text, versionFrom,
versionTo, int.MaxValue, true, true);
List<Changeset> changesets = results.Cast<Changeset>().ToList();
if (0 < changesets.Count)
{
foreach (Changeset changeset in changesets)
{
Change[] changes = changeset.Changes;
richTextBox1.Text +=" Files contained in " +
changeset.ChangesetId + " at " + changeset.CreationDate +
" with comment " + changeset.Comment + " Changer "+ changeset.Owner;
richTextBox1.Text += System.Environment.NewLine;
foreach (Change change in changes)
{
string serverItem = change.Item.ServerItem;
richTextBox1.Text += serverItem + " " + change.ChangeType;
richTextBox1.Text += System.Environment.NewLine;
}
richTextBox1.Text += System.Environment.NewLine;
}
}
}