如何使用TFS API 2013获取所有迭代路径

aqw*_*ert 2 c# api tfs tfs2013

我已经使用TFS API库一段时间了,并且在与TFS 2010连接时使用了以下代码来获取迭代路径(来自此页面的代码)...

public IList<string> GetIterationPaths(Project project)
{
   List<string> iterations = new List<string>();
   foreach (Node node in project.IterationRootNodes)
      AddChildren(string.Empty, node, iterations);
   return iterations;
}

private void AddChildren(string prefix, Node node, List<string> items)
{
   items.Add(node.Path);
   foreach (Node item in node.ChildNodes)
      AddChildren(prefix + node.Name + "/", item, items);
}
Run Code Online (Sandbox Code Playgroud)

当我在TFS 2013中查看所有迭代时,事情发生了变化.在TFS 2013中,迭代的概念略有改变,而TFS 2013的API程序集则没有IterationRootNodes

我使用以下代码来获得团队迭代,但这是基于团队的,而不是针对项目的完整迭代集...(目前获得第一个团队但可以编码以获得其他团队)

 var cred = new TfsClientCredentials(new WindowsCredential(), true);
 TfsTeamProjectCollection coll = new TfsTeamProjectCollection("{URI to SERVER}", cred);
 coll.EnsureAuthenticated();

 var teamConfig = coll.GetService<TeamSettingsConfigurationService>();
 var css = coll.GetService<ICommonStructureService4>();
 var project = css.GetProjectFromName(projectInfo.ProjectName);

 IEnumerable<TeamConfiguration> configs = teamConfig.GetTeamConfigurationsForUser(new[] { project.Uri.ToString() });
 TeamConfiguration team = configs.FirstOrDefault(x => x.ProjectUri == project.Uri.ToString());

 var iterations = BuildIterationTree(team, css);
Run Code Online (Sandbox Code Playgroud)

这里BuildIterationTree看起来像...

 private IList<IterationInfo> BuildIterationTree(TeamConfiguration team, ICommonStructureService4 css)
    {
        string[] paths = team.TeamSettings.IterationPaths;

        var result = new List<IterationInfo>();

        foreach (string nodePath in paths.OrderBy(x => x))
        {
            var projectNameIndex = nodePath.IndexOf("\\", 2);
            var fullPath = nodePath.Insert(projectNameIndex, "\\Iteration");
            var nodeInfo = css.GetNodeFromPath(fullPath);
            var name = nodeInfo.Name;
            var startDate = nodeInfo.StartDate;
            var endDate = nodeInfo.FinishDate;

            result.Add(new IterationInfo
            {
                IterationPath = fullPath.Replace("\\Iteration", ""),
                StartDate = startDate,
                FinishDate = endDate,
            });
        }
        return result;
    }
Run Code Online (Sandbox Code Playgroud)

我的问题是......如何获得完整的迭代树而不是TFS 2013的Team特定迭代?

团队特定的迭代可以配置为通过Web Portal通过复选框显示或不显示迭代.

Eri*_*ric 5

你的问题回答了我关于如何获得团队特定迭代的问题,所以我能做的最少就是提供获得完整迭代层次结构的代码片段.我想我最初在这个博客上发现了这个代码:

    public static void GetIterations(TfsTeamProjectCollection collection,
        ICommonStructureService4 css, ProjectInfo project)
    {
        TeamSettingsConfigurationService teamConfigService = collection.GetService<TeamSettingsConfigurationService>();
        NodeInfo[] structures = css.ListStructures(project.Uri);
        NodeInfo iterations = structures.FirstOrDefault(n => n.StructureType.Equals("ProjectLifecycle"));
        XmlElement iterationsTree = css.GetNodesXml(new[] { iterations.Uri }, true);

        string baseName = project.Name + @"\";
        BuildIterationTree(iterationsTree.ChildNodes[0].ChildNodes, baseName);
    }

    private static void BuildIterationTree(XmlNodeList items, string baseName)
    {
        foreach (XmlNode node in items)
        {
            if (node.Attributes["NodeID"] != null &&
                node.Attributes["Name"] != null &&
                node.Attributes["StartDate"] != null &&
                node.Attributes["FinishDate"] != null)
            {
                string name = node.Attributes["Name"].Value;
                DateTime startDate = DateTime.Parse(node.Attributes["StartDate"].Value, CultureInfo.InvariantCulture);
                DateTime finishDate = DateTime.Parse(node.Attributes["FinishDate"].Value, CultureInfo.InvariantCulture);

                // Found Iteration with start / end dates
            }
            else if (node.Attributes["Name"] != null)
            {
                string name = node.Attributes["Name"].Value;

                // Found Iteration without start / end dates
            }

            if (node.ChildNodes.Count > 0)
            {
                string name = baseName;
                if (node.Attributes["Name"] != null)
                    name += node.Attributes["Name"].Value + @"\";

                BuildIterationTree(node.ChildNodes, name);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)