Getting Build Definition Steps with Visual Studio Team Services .NET API

One of the features that I love the most about Visual Studio Team Services is the ability to build my code in the cloud. In my project I have a requirement for dynamic build provisioning, which works well. However, I recently tried to figure out how can I get the list of steps from a build definition, and was hitting a roadblock up until I got some help from Chris Patterson.

Visual Studio Team Services

I am using the .NET client libraries for accessing the Team Services capabilities (you can easily get the right packages through NuGet).

NuGet Team Services

In my code, I was using the standard call to get the a list of definitions:

var credentials = new VssBasicCredential(UserName, Token);
var buildClient = new BuildHttpClient(new Uri(Url), credentials);
var definitions = await buildClient.GetDefinitionsAsync(project: parameters["project"]);

However, for each definition I would only get a BuildDefinitionReference instance:

Build Definition Reference

So what’s missing here? Build steps. Of course, there is the REST API that you can leverage for this scenario, but in this case you’d have to write a custom implementation of the reader (parse out JSON, select the right node, and then transform raw JSON into a list of objects).

Luckily, the right functionality is already built into the SDK, and I wrote a simple helper method to do just what I needed in terms of getting the build steps:

public async Task<IEnumerable> GetBuildDefinitionSteps(IDictionary<string, string> parameters)
{
    var buildClient = new BuildHttpClient(new Uri(Url), new VssBasicCredential(UserName, Token));
    var definition =
        await buildClient.GetDefinitionAsync(parameters["project"], Parse(parameters["definitionId"]));
 
 
    return definition.Steps.Select(step => new BuildStep {Name = step.DisplayName, Inputs = step.Inputs}).ToList();
}

As long as you have the definition ID (this is not the name, but rather the numeric identifier), you can successfully get a list of steps as now you will bet getting a real BuildDefinition instance that has the Steps property:

Build Definition Class

One thought on “Getting Build Definition Steps with Visual Studio Team Services .NET API”

Leave a Reply

Your email address will not be published. Required fields are marked *