5

I need to get current iteration's path from TFS project. I'm able to use REST API query <server>/<project>/_apis/work/teamsettings/iterations?$timeframe=current&api-version=v2.0-preview but I don't want to perform query and parse JSON response. I want to use appropriate API in .NET client libraries for VSTS (and TFS).

I have an instance of the VssConnection. How can I get the path of current iteration from this object?

Maxim
  • 1,995
  • 1
  • 19
  • 24

4 Answers4

3

You can get the current iteration using the WorkHttpClient without having to iterate:

var creds = new VssBasicCredential(string.Empty, "personalaccesstoken");
VssConnection connection = new VssConnection(new Uri("url"), creds);
var workClient = connection.GetClient<WorkHttpClient>();
var teamContext = new TeamContext(teamId);
teamContext.ProjectId = projectId;
var currentIteration = await workClient.GetTeamIterationsAsync(teamContext, "current");
jvilalta
  • 6,679
  • 1
  • 28
  • 36
2

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;
}

And returning the TeamSettings.CurrentIterationPath property.

CARLOS LOTH
  • 4,675
  • 3
  • 38
  • 44
0

I found a solution using VssConnection:

var workClient = connection.GetClient<WorkHttpClient>();
var iterations = workClient.GetTeamIterationsAsync(new TeamContext("project-name")).Result;

var currentDate = DateTime.Now.Date;
var currentIterationPath = iterations
    .Select(i => new { i.Path, i.Attributes })
    .FirstOrDefault(i => currentDate >= i.Attributes.StartDate &&
                         currentDate <= i.Attributes.FinishDate)
    ?.Path;
Maxim
  • 1,995
  • 1
  • 19
  • 24
  • I think loading all iteration paths and iterating through them won't have a good performance on large team projects. – CARLOS LOTH May 24 '18 at 14:56
-1

Here is a case provides a solution: Get the current iteration path from TFS

 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;
                }
            }
        }
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39