DCSIMG
How To Get A File History In TFS Source Control Using Code - SRL Group

SRL Group

This blog is about Team System, QA and Development methodologies and more...

VSTS Resources

Team System Bloggers

How To Get A File History In TFS Source Control Using Code

A project I am doing these days requires me to get file history changesets using the API.

Sounds like a simple task right?

Apparently there is not a lot of reference to that in Google, so after almost throwing my computer out of the window (More than once I most admit) I found the answer.

Add the following references to your using list:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using System.Windows.Forms;

The example function is very simple it gets a change and shows message boxes of all the changesets that have a change for the specified file up to the change transferred to the method.

Note: Change the [Server Name] with your TFS name.

public void GetChangesetsOfFile(Change theChange)

    {  

      //Query History parameters

      TeamFoundationServer tfs new TeamFoundationServer
                   
("[Server Name]");

     
VersionControlServer VCServer =
                    (VersionControlServer)tfs.GetService
                    (typeof(VersionControlServer));
 

      int changeId = (theChange.Item.DeletionId != 0) ?
                   
theChange.Item.ChangesetId - 1 : 
                     theChange.Item.ChangesetId;

      ChangesetVersionSpec version = new 
                            ChangesetVersionSpec(changeId);

      ChangesetVersionSpec versionFrom = new
                          
 ChangesetVersionSpec(1);

      string path = theChange.Item.ServerItem;

 

      //Query History Command

      IEnumerable changesets = VCServer.QueryHistory(path,
               version, 0, RecursionType.None, null,
               versionFrom, LatestVersionSpec.Latest,
               int
.MaxValue, true, false);

 

      foreach (Changeset cSet in changesets) 
     
        MessageBox.Show(cSet.Changes
            [0].Item.ChangesetId.ToString()); 
     
    }

I hope I could help.

Have Fun!!!

Comments

Team System News said:

Martin Woodward on Slotting into TFS 2010 The SRL Team Blog on How To Get A File History In TFS Source

# June 23, 2009 2:59 PM

Alexey P. said:

I am new to TFS and looking for all the info I can get.

Where do you get theChange object from?

I am looking for a way to get a list of all files affected between certain dates and use that data to get a list of modified project names. The build engineer is then going to use that info to compile and build those projects.

Thanks,

Alexey

# July 9, 2009 5:40 PM

Eran Ruso said:

Alexey P.

I didn't understend if you need to do it thru the UI or using the API.

Thru the UI it is a very easy task, use the source control, right click the project name and see the changes thru view history.

Using the API the QueryHistory command is the right command for the task, thru it you can get all the files that has changed between dayes, than track the source of the project in order to know what build definition to use and fire the build using the "command line" command.

Hope it helped!!!

# July 11, 2009 11:28 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: