TFS API Part 37 – Create Builds
This is the last of series of 3 about manipulating Build Controllers (TFS API Part 35 – Create Build Controllers), Agents and Definitions (TFS API Part 36 – Create Build Definitions) and now I’ll complete the series with a demonstration on how to create a new Build and set the Result using TFS API.
This can assist you in building Fake definition in order to publish tests or setting a test environment much more easily.
Download Demo Project


As you can see from the picture above you can define the Build Name and Status and the result will appear under the Build Definition in TFS.

Step 1: Define “Update Build Information” Permissions
Before you can do any manipulation on the Build item you need to have the proper permissions, in Team Explorer under the desire Team Project go to the Builds node and Right Click –> Security
There make sure you user or group have the “Update build Information” box checked.

Step 2: Create New Build
The BuildDefinition item has CreateManualBuild method that will serve us for creating new Build.
private void BtnAddBuildClick(object sender, RoutedEventArgs e)
{
if (listAddDefinitionBuildDefinitions.SelectedItem == null) return;
if(string.IsNullOrEmpty(txtBuildName.Text) || string.IsNullOrEmpty(txtLocalPath.Text) ||
string.IsNullOrEmpty(txtServerPath.Text) || comboBuildStatus.SelectedItem == null)
{
MessageBox.Show("Please make sure the following fields has valid value:\n1.Build Name\n2.Local Path\n3.Server Path\n4.Status",
"Missing Values", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
try
{
var buildDefinition = (IBuildDefinition)listAddDefinitionBuildDefinitions.SelectedItem;
IBuildDetail buildDetail = buildDefinition.CreateManualBuild(txtBuildName.Text);
IBuildProjectNode buildProjectNode = buildDetail.Information.AddBuildProjectNode(
DateTime.Now.AddSeconds(10), // Finish Time = The time at which the project finished building.
comboFlavor.SelectedValue.ToString(), //Flavor = The flavor (configuration) the project was built for.
txtLocalPath.Text, //Local Path = The local path of the project file.
comboPlatform.SelectedValue.ToString(), //Platform = The platform the project was built for.
txtServerPath.Text, // Server Path = The server path of the project file.
DateTime.Now, //Start Time = The time at which the project was built.
"default"); //Target Name = The targets for which the project was built.
buildProjectNode.Save();
buildDetail.FinalizeStatus((BuildStatus)comboBuildStatus.SelectedItem);
ClearAddBuildForm();
}
catch (AccessDeniedException accessDeniedException)
{
MessageBox.Show(accessDeniedException.Message, "Access Denied Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (BuildNumberAlreadyExistsException buildNumberAlreadyExistsException)
{
MessageBox.Show(buildNumberAlreadyExistsException.Message, "Build Number Already Exists Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
catch (InvalidFinalStatusException invalidFinalStatusException)
{
MessageBox.Show(invalidFinalStatusException.Message, "Invalid Final Status Exception", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
listBuilds.ItemsSource = ((IBuildDefinition)listAddDefinitionBuildDefinitions.SelectedItem).QueryBuilds();
}
}
Step 3: Query Builds from Build Definition
Using the QueryBuilds method in IBuildDefinition will bring back all Build under that specific Build Definition.
private void ListAddDefinitionBuildDefinitionsSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if(listAddDefinitionBuildDefinitions.SelectedItem == null) return;
listBuilds.ItemsSource = ((IBuildDefinition) listAddDefinitionBuildDefinitions.SelectedItem).QueryBuilds();
}
Step 4: Remove Build
IBuildServer allow you to call the DeleteBuilds method with the Builds you want to delete.
private void BtnRemoveBuildClick(object sender, RoutedEventArgs e)
{
if (listBuilds.SelectedItem == null) return;
_buildServer.DeleteBuilds(new IBuildDetail[] { (IBuildDetail)listBuilds.SelectedItem });
listBuilds.ItemsSource = ((IBuildDefinition)listAddDefinitionBuildDefinitions.SelectedItem).QueryBuilds();
}