Search code examples
c#.nettfstfs-sdk

Get the current iteration path from TFS


I'm trying to get the current iteration path for the teams TFS project. The way I'm trying to do this is by using the blog from http://blog.johnsworkshop.net/tfs11-api-reading-the-team-configuration-iterations-and-areas/ . I start by getting the team configurations from the following code:

        TfsTeamProjectCollection tpc = TFSConncetion(@"http://tfs/url");
        var configSvc = tpc.GetService<TeamSettingsConfigurationService>();
        var configs = configSvc.GetTeamConfigurationsForUser(projectUri);

The problem with this is that my configs is always null, even though I'm a member of the team. I'm positive my projects URI is correct as well. After this I would get the team settings and use that to display the current iteration path.

TeamSettings ts = config.TeamSettings;
Console.WriteLine(ts.CurrentIterationPath);

Even if this didn't work I can still query the iteration dates from the team setting to get the one iteration that has a start date before today and finish date after today. The main problem is that I can't get my TeamSettingsConfigurationService to return anything but null when I try to get the team configurations with my projects URI.


Solution

  • I actually got the answer myself without using TeamSettingsConfigurationService at all. Here's how I did it:

        private static XmlNode currentIterationNode;
        TfsTeamProjectCollection tpc = TFSConncetion(@"http://tfs/url");
    
        ICommonStructureService4 css = tpc.GetService<ICommonStructureService4>();;
        WorkItemStore workItemStore = new WorkItemStore(tpc);
    
            foreach (Project teamProject in workItemStore.Projects)
            {
                if (teamProject.Name.Equals("TeamProjectNameGoesHere"))
                {
                    NodeInfo[] structures = css.ListStructures(teamProject.Uri.ToString());
                    NodeInfo iterations = structures.FirstOrDefault(n => n.StructureType.Equals("ProjectLifecycle"));
    
                    if (iterations != null)
                    {
                        XmlElement iterationsTree = css.GetNodesXml(new[] { iterations.Uri }, true);
                        XmlNodeList nodeList = iterationsTree.ChildNodes;
                        currentIterationNode = FindCurrentIteration(nodeList);
                        String currentIterationPath = currentIterationNode.Attributes["Path"].Value;
                    }
                }
            }
    

    Where currentIterationPath is the current iteration path from TFS. The key to doing this was to get the NodeInfo[] array of structures and the NodeInfo iterations from these two lines of code I got from chamindacNavantis https://social.msdn.microsoft.com/Forums/vstudio/en-US/4b785ae7-66c0-47ee-a6d2-c0ad8a3bd420/tfs-get-iteration-dates-metadata?forum=tfsgeneral:

        NodeInfo[] structures = css.ListStructures(teamProject.Uri.ToString());
        NodeInfo iterations = structures.FirstOrDefault(n => n.StructureType.Equals("ProjectLifecycle"));
    

    After that I created an xml with nodes of every iteration inside the team project. These nodes also have the start date and end dates of each iteration. So I checked each node for a start date before DateTime.Now and finish date after DateTime.Now, which is all FindCurrentIteration(nodeList) does. And that will give you the current iteration node.