TFS 2010: Getting list of changeset Ids
Asked Answered
M

2

18

I need to create two methods as follows:

  1. Retrieve all changesets in TFS.
  2. Retrieve all changesets newer than a specified changeset.

I've done some google searching and found a few links and managed to come up with some code. I can't seem to work out the method to call to get the complete list of changeset items. I've botched something together to get this but was wondering if someone can help me:

TfsTeamProjectCollection projectCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://mydomain.com:8080/tfs"));
VersionControlServer versionControl = projectCollection.GetService<VersionControlServer>();
int latestId = versionControl.GetLatestChangesetId();

List<Changeset> changesetList = new List<Changeset>();
for (int i = 1; i < latestId; i++)
{
    try
    {
        Changeset cs = versionControl.GetChangeset(i);
        if (cs != null)
        {
            changesetList.Add(cs);
        }
    }
    catch (ResourceAccessException)
    {
    }
}

Getting the changeset for some Id's throws a 'ResourceAccessException' exception which is why the handler has been added.

Any ideas on how to do this in the "proper" way?

I'm using Visual Studio 2010 with TFS 2010. Application is being written in C# as a .Net 4.0 app.

TIA

Melchor answered 13/12, 2011 at 17:50 Comment(2)
Sounds like a user permission issue. Have you tried the overload for GetTeamProjectCollection to which you can pass user credentials? Otherwise it will be using the credentials of the user executing the code.Varus
The way @Morten has proposed should work out for you. Your failure might be caused by deleted changesets (msdn.microsoft.com/en-us/library/bb386005.aspx). I'd mark the failing ones & go look in Source Control Explorer what's so special about them.Greece
D
28

Try this:

  // Replace with your setup
  var tfsServer = @"http://tfsserver:8080/tfs/SW";
  var serverPath = @"$/PCSW/ProjectX/Main";

  // Connect to server
  var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer));
  tfs.Connect(ConnectOptions.None);
  var vcs = tfs.GetService<VersionControlServer>();

  // Create versionspec's. Example start with changeset 529
  VersionSpec versionFrom = VersionSpec.ParseSingleSpec("C529", null);
  // If you want all changesets use this versionFrom:
  // VersionSpec versionFrom = null;
  VersionSpec versionTo = VersionSpec.Latest;

  // Get Changesets
  var changesets = vcs.QueryHistory(
    serverPath,
    VersionSpec.Latest,
    0,
    RecursionType.Full,
    null,
    versionFrom,
    versionTo,
    Int32.MaxValue,
    true,
    false
    ).Cast<Changeset>();
David answered 13/12, 2011 at 19:10 Comment(2)
To get the complete repository use this: var serverPath = @"$/"; If you only want part of the repository, select the path shown in "Source Control Explorer".David
If I want to get all changeset no's for specific date then what changes required in abouve solution?Horntail
F
1

In case it's only the changeset numbers in the range that are required, the following code may be used.

foreach (var changeset in changesets)
{ 
   Console.WriteLine(changeset.ChangesetId); 
}
Ferdie answered 7/5, 2014 at 19:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.