TFS API Part 36 – Create Build Definitions


In my last post TFS API Part 35 – Create Build Controllers  I showed how to create Build Controllers using TFS API, this can help create a Fake Build and publish test results outside TFS.

This post continue the coding from me previous post, and shows how to create or delete Build Definitions under a specific Team Project.

Download Demo Project


Step 1: Obtain Build Process Templates and Existing Definition from Team Project

private void ListDefinitionsTeamProjectSelectionChanged(object sender, SelectionChangedEventArgs e)
if (listDefinitionsTeamProject.SelectedItem == null) return;
     comboProcessTemplates.ItemsSource = _buildServer.QueryProcessTemplates(
listAddDefinitionBuildDefinitions.ItemsSource = _buildServer.QueryBuildDefinitions(
         ((Project)listDefinitionsTeamProject.SelectedItem).Name); }

Step 2: Add New Build Definition

private void BtnAddDefinitionClick(object sender, RoutedEventArgs e)
if (listDefinitionsTeamProject.SelectedItem != null && listTfsControllers.SelectedItem != null
if (string.IsNullOrEmpty(txtDefinitionName.Text) || string.IsNullOrEmpty(txtDropLocation.Text) ||
            comboProcessTemplates.SelectedItem == null)       
                 "Please make sure the following fields has value:\n1.Definiton Name\n2.Drop Location\n3.Process Template",
  "Missing Values", MessageBoxButton.OK, MessageBoxImage.Error);            
        var proj = (Project)listDefinitionsTeamProject.SelectedItem;
            var controller = (IBuildController)listTfsControllers.SelectedItem; 
            //Create new instance of build definition under the Team Project.    
         var definition = _buildServer.CreateBuildDefinition(proj.Name);         
    definition.Name = txtDefinitionName.Text;           
//Define Build Definition Trigger - // All, Batch , Gated, Individual, None, Schedule, ScheduleForced  
           definition.ContinuousIntegrationType = (ContinuousIntegrationType)comboContinuousIntegrationTypes.SelectedItem;     
        //Define Build Controller           
definition.BuildController = controller;          
   definition.DefaultDropLocation = txtDropLocation.Text;
            definition.Description = txtDescription.Text;    
         definition.Enabled = (bool)chxIsEnabled.IsChecked ? true : false;        
     //Create a fake workspace folder         
    definition.Workspace.AddMapping("$/", "c:\\someFakeFolder", WorkspaceMappingType.Map); 
            //Assign the Process Template for the new build definition           
definition.Process = (IProcessTemplate)comboProcessTemplates.SelectedItem;        
//Refresh the Build Definitions List        
     listAddDefinitionBuildDefinitions.ItemsSource = _buildServer.QueryBuildDefinitions(((Project)listDefinitionsTeamProject.SelectedItem).Name);
catch (BuildServerException ex)  
MessageBox.Show(ex.Message, "Opps", MessageBoxButton.OK, MessageBoxImage.Error);  
    } }

Step 3: Remove Build Definition

private void BtnRemoveDefinitionClick(object sender, RoutedEventArgs e)
if (listAddDefinitionBuildDefinitions.SelectedItem == null) return;
_buildServer.DeleteBuildDefinitions(new IBuildDefinition[] { (IBuildDefinition)listAddDefinitionBuildDefinitions.SelectedItem });

  listAddDefinitionBuildDefinitions.ItemsSource = _buildServer.QueryBuildDefinitions(((Project)listDefinitionsTeamProject.SelectedItem).Name); }


Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. shair24/07/2012 ב 14:28


    There is a problem with all files, please try again in few days.