We are going to learn how to connect Team Foundation Server/Visual Studio Online using a .Net Console Application in this article.

Create a project

  1. Open Visual Studio
  2. On the start window, choose Create a new project.
  3. On the Create a new project window, enter or type console in the search box. Next, choose Console App (.NET Framework) Visual C# from the list, and then click Ok.

Install and use a package

NuGet packages contain reusable code that other developers make available to you for use in your projects.

  1. In Solution Explorer, right-click References and choose Manage NuGet Packages.
  2. Choose “nuget.org” as the Package source, select the Browse tab, search for Microsoft.TeamFoundationServer.Client, select that package in the list, and select Install
  3. Select the Browse tab, search for Microsoft.TeamFoundationServer.ExtendedClient, select that package in the list, and select Install:

Add code

Make sure to include the following headers in your Program.cs file

  • Microsoft.TeamFoundation.Client
  • Microsoft.TeamFoundation.SourceControl.WebApi
  • System
  • System.Collections.Generic
  • System.IO
  • System.Linq
  • System.Net
  • System.Xml.Linq

Add the following code to connect your console application code with TFS and make sure you are able to generate the client context.

TfsTeamProjectCollection context = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://{yourtfs}.visualstudio.com"));
NetworkCredential myCred = new NetworkCredential("your_username", "your_password");
context.Credentials = myCred;
TfvcHttpClient client = context.GetClient<TfvcHttpClient>();

Add the following code to get all the files within the given project path

List<TfvcItem> items = client.GetItemsAsync("$/Sample/Data", VersionControlRecursionType.None).Result;
foreach(TfvcItem item in items.Where(f => f.IsFolder == false))
    Console.WriteLine($"Path => {item.Path} Change Date => {item.ChangeDate} Changeset Version => {item.ChangesetVersion}");

Add the following code to get the file content for the given file path

Stream fileContent = client.GetItemTextAsync("\$/Sample/Data/sample.xml").Result;
XDocument glossaryXml = XDocument.Load(fileContent);

Add the following code to get the changesets applied on the given file path

TfvcChangesetSearchCriteria searchCriteria = new TfvcChangesetSearchCriteria();
searchCriteria.ItemPath = "\$/Sample/Data/sample.xml";
List<TfvcChangesetRef> changeSets = client.GetChangesetsAsync(maxCommentLength: null, skip: null,
top: null, orderby: null, searchCriteria: searchCriteria).Result;

Add the following code to filter the changesets applied by a specific code author

List<string> authors = new List<string>();
authors.Add("Joseph Velliah");
List<TfvcChangesetRef> changeSetsFilteredByAuthor = changeSets.Where(a => authors.Contains(a.Author.DisplayName)).ToList();

Add the following code to get the file content of a specific file version based on changeset ID

List<TfvcChange> changesetChanges = client.GetChangesetChangesAsync(changeSetsFilteredByAuthor[0].ChangesetId).Result;
TfvcVersionDescriptor versionDescriptor = new TfvcVersionDescriptor(versionOption: null, versionType: TfvcVersionType.Changeset,
version: changeSetsFilteredByAuthor[0].ChangesetId.ToString());

List<TfvcChange> changesetChangesFilteredByPath = changesetChanges.
Where(f => f.Item.Path.Equals("\$/Sample/Data/sample.xml")).ToList();

Stream content = client.GetItemContentAsync(path: changesetChangesFilteredByPath[0].Item.Path, versionDescriptor: versionDescriptor).Result;

Hope you found this article helpful! Let me know if I might have missed anything or can be done better.