Get the current iteration path from TFS

不羁的心 提交于 2019-12-10 14:19:45

问题


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.


回答1:


There must be something wrong with your server connection or the project uri you're passing as the other code looks okay.

Maybe try something like this:

TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://server:8080/tfs/collection"),
                          new System.Net.NetworkCredential(tfsUserName, tfsPassword));
tpc.EnsureAuthenticated();

Connect to Team Foundation Server from a Console Application

There is a good sample here which you can download (WPF client) and it will allow you to select a server connection, Team Project and Team:

TFS API Part 46 (VS11) – Team Settings

You can step through it and check the values you're passing into your code.

The sample gets the team configuration information is the same way you have in your code.

TeamSettingsConfigurationService teamConfig = tfs.GetService<TeamSettingsConfigurationService>();
    var configs = teamConfig.GetTeamConfigurationsForUser(new[] { projectInfo.Uri });

Once you have the collection of TeamConfiguration items then you need TeamSettings.CurrentIterationPath




回答2:


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.




回答3:


The simplest way I've found to do it was by using ICommonStructureService4 and TeamSettingsConfigurationService methods:

static TfsTeamProjectCollection _tfs = TfsTeamProjectCollectionFactory
    .GetTeamProjectCollection("<tfsUri>")

(...)

static string GetCurrentIterationPath()
{
    var css = _tfs.GetService<ICommonStructureService4>();

    var teamProjectName = "<teamProjectName>";
    var project = css.GetProjectFromName(teamProjectName);

    var teamName = "<teamName>";
    var teamSettingsStore = _tfs.GetService<TeamSettingsConfigurationService>();

    var settings = teamSettingsStore
        .GetTeamConfigurationsForUser(new[] { project.Uri })
        .Where(c => c.TeamName == teamName)
        .FirstOrDefault();

    if (settings == null)
    {
        var currentUser = System.Threading.Thread.CurrentPrincipal.Identity.Name;
        throw new InvalidOperationException(
            $"User '{currentUser}' doesn't have access to '{teamName}' team project.");
    }

    return settings.TeamSettings.CurrentIterationPath;
}


来源:https://stackoverflow.com/questions/31417572/get-the-current-iteration-path-from-tfs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!