Azure DevOps - Get the capacity of your team in the current sprint programmatically

Microsoft Azure DevOps provides a solid way of tracking your team-progress while using the scrum principles.

Nuget Packages with WebApi Clients are provided for accessing your team information via code. For example you can access the queries of your team, the work items of your backlog and much more!

In this article we will focus on getting the capacity of the current sprint for each member of your team. This is the code snippet that does our job:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.TeamFoundation.Core.WebApi.Types;
    using Microsoft.TeamFoundation.Work.WebApi;
    using Microsoft.VisualStudio.Services.Common;
    using Microsoft.VisualStudio.Services.WebApi;

    static async System.Threading.Tasks.Task Main(string[] args)
    {
        Uri azureDevOpsUrl = new Uri("https://dev.azure.com/YOUR_COMPANY_NAME");

        // replace this based on the number of days per sprint
        int NUMBER_OF_DAYS_PER_SPRINT = 10;

        // Create a new personal access token from the Azure DevOps site
        string PERSONAL_ACCESS_TOKEN = "YOUR_PERSONAL_ACCESS_TOKEN";

        using VssConnection vssConnection = 
            new VssConnection(azureDevOpsUrl, new VssBasicCredential(string.Empty, PERSONAL_ACCESS_TOKEN));

        await vssConnection.ConnectAsync();

        // Set the source team context
        TeamContext sourceTeamContext = new TeamContext("THE_NAME_OF_YOUR_PROJECT", "THE_NAME_OF_YOUR_TEAM");

        WorkHttpClient workClient = vssConnection.GetClient<WorkHttpClient>();

        // result contains all the iterations, old & new. We want to pick the current iteration
        List<TeamSettingsIteration> result = await workClient.GetTeamIterationsAsync(sourceTeamContext);

        // Find the single current iteration by checking the TimeFrame attribute
        TeamSettingsIteration currentIteration = result.Single(x => x.Attributes.TimeFrame == TimeFrame.Current);

        List<TeamMemberCapacityIdentityRef> teamMembers = 
            await workClient.GetCapacitiesWithIdentityRefAsync(sourceTeamContext, currentIteration.Id);

        foreach (var teamMember in teamMembers)
        {
            // The daysOff come into a DateRange (from - to) format
            int daysOff = (int)teamMember.DaysOff.Sum(a => (a.End - a.Start).TotalDays + 1);

            // You can also access the Activities by name
            float sprintCapacity = (teamMember.Activities.
                        Select(a => a.CapacityPerDay).Sum())
                            * (NUMBER_OF_DAYS_PER_SPRINT - daysOff);

            Console.WriteLine(daysOff);
            Console.WriteLine(sprintCapacity);
        }
    }

Thats it. For here on you can expand this script and add more functionality into it. For example filter the capacity based on a specific activity (development, deployment, etc.), or find out the total days off of your team.

comments powered by Disqus