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!!!