Fetching file history from TFS : the code way

These days I’m writing a tool for Visual Studio that integrates with Team Foundation Server. This post is about how to retrieve/fetch history of a file from source control server using TFS-SDK. Following function takes server name and file path as input and returns an enumerator holding Changeset objects.

public static IEnumerable FetchHistory(string serverName, string filePath)
TeamFoundationServer tfs = TeamFoundationServerFactory.GetServer(serverName);
VersionControlServer vcs = tfs.GetService(typeof(VersionControlServer)) as VersionControlServer;
ExtendedItem[] itemArray1 = vcs.GetExtendedItems(filePath, DeletedState.NonDeleted, ItemType.Any);

if ((itemArray1 == null) || (itemArray1.Length == 0))
throw new Exception(“There are no items.”);

ExtendedItem item1 = itemArray1[0];
IEnumerable enumerable1 = vcs.QueryHistory(filePath,
VersionSpec.Latest, item1.DeletionId,
(item1.ItemType == ItemType.Folder) ? RecursionType.Full : RecursionType.None,
“”, new ChangesetVersionSpec(1),
VersionSpec.Latest, 0x7fffffff, true, false);
return enumerable1;

To compile this successfully you also need to add references of following two assemblies found at [VS installation directory]\Common7\IDE\PrivateAssemblies\:


Call to this function should be made as follows:

FetchHistory(“[serverName]”, “$/Project1/Class.cs” );

– Ankit

3 thoughts on “Fetching file history from TFS : the code way

  1. Pingback: TFS – Free Text search in History Objects - Ye Meri Life Hai!

  2. Tarun Sukhu

    Hi Ankit,

    Your project looks interesting. What level of integration have you achieved. I plan to work on a project where we need to pump in different items (source control , bug information , workflow) from an external system into TFS. Any tips would be good.

Leave a Reply

Your email address will not be published. Required fields are marked *