Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current iteration from TFS

Tags:

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?

like image 814
Maxim Avatar asked Dec 19 '17 12:12

Maxim


2 Answers

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");
like image 89
jvilalta Avatar answered Sep 19 '22 13:09

jvilalta


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.

like image 30
CARLOS LOTH Avatar answered Sep 21 '22 13:09

CARLOS LOTH