Get all iteration paths in TFS programtically

A common way to organize work items in TFS is to assign them to different iterations. I’m using this with the scum model where every sprint corresponds to one iteration. So how can we get all iteration paths in TFS to use them in our own implementation. The information is not saved on the work items, we will need to look directly at the project object and recursively iterate on the iteration object tree to get all iteration paths.

Example project
TFSIterationPath
In this example you will be able to connect to a TFS server and get all work items based on project and iteration path listed.

And now the code

public ICollection GetIterationPaths(string projectName, Uri teamCollectionUrl)
{
    var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(teamCollectionUrl);
    var store = (WorkItemStore) tfs.GetService(typeof (WorkItemStore));
    var result = new List();
 
    foreach (var project in store.Projects.Cast().
        Where(project => string.Compare(project.Name, projectName, StringComparison.OrdinalIgnoreCase) == 0))
    {
        foreach (Node node in project.IterationRootNodes)
        {
            var path = project.Name + "\\" + node.Name;
            result.Add(path);
            RecursiveAddIterationPath(node, result, path);
        }
 
        break;
    }
 
    return result;
}
 
private static void RecursiveAddIterationPath(Node node, ICollection result, string parentIterationName)
{
    foreach (Node item in node.ChildNodes)
    {
        var path = parentIterationName + "\\" + item.Name;
        result.Add(path);
        if (item.HasChildNodes)
        {
            RecursiveAddIterationPath(item, result, path);
        }
    }
}

You can find the complete source code here: TFSIterationPath

3.00 avg. rating (68% score) - 2 votes

About Peter Wibeck

Comments

5 Responses to “Get all iteration paths in TFS programtically”
  1. Dominic says:

    You are my hero of the day! Thanks a lot!
    I searched over and over how to get a list of Iterations!

    Once again thanks!

  2. Paul says:

    Thanks for this post – it gave me the example I needed… but I do have a question :

    I want to retrieve a list of iterations names/paths but I also want the start/end date of the sprints. I can’t find where this sits in the object model. Any ideas?

  3. Bill says:

    This code doesn’t compile

    • Bill says:

      I take that back. Didn’t see the sample code and just had to reset a reference to Microsoft.TeamFoundation.Client. Thanks

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

*