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
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
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!
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?
Found this question on stackoverflow that seems to cover your problem.
This code doesn’t compile
I take that back. Didn’t see the sample code and just had to reset a reference to Microsoft.TeamFoundation.Client. Thanks