TFS API Part 35 – Create Build Controllers

08/05/2011

In my previous post on TFS API is showed some basic actions for TFS Build API – TFS API Part 34– Build Basic Actions

In this series I’ll some some more cool things in Build 2010 API, the first thing is a simple flow on how to get the all build definitions from Team Projects, how to obtain the Build Controller from the definition and how to get the Build Agents available in each Controller.

Download Demo Project

image

Than we can move forward to how you can use TFS API to create and delete Build Controllers programmatically, even for Fake Controllers (I’ll take why later).

image

Step 1: Create Project and Add Reference

Create an WPF/WinForm application and add the following references:

First add reference for

  • Microsoft.TeamFoundation.dll
  • Microsoft.TeamFoundation.Build.Client
  • Microsoft.TeamFoundation.Build.Common.dll
  • Microsoft.TeamFoundation.dll
  • Microsoft.TeamFoundation.WorkItemTracking.Client.dll

All files located under – c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ReferenceAssemblies\v2.0\

Step 2: Connect to Team Foundation Server

private void BtnConnectTfsClick(object sender, RoutedEventArgs e)
{     
using (var tpp = new TeamProjectPicker(TeamProjectPickerMode.NoProject, false))
{      
tpp.ShowDialog();  
      if (tpp.SelectedTeamProjectCollection == null) return;  
      _server = tpp.SelectedTeamProjectCollection;  
      _server.EnsureAuthenticated();    
    listProjects.ItemsSource = ((WorkItemStore) _server.GetService(typeof(WorkItemStore))).Projects;  
     _buildServer = (IBuildServer)_server.GetService(typeof(IBuildServer));
     listTfsControllers.ItemsSource = _buildServer.QueryBuildControllers();
      tabControl.IsEnabled = true;    
     }
}

Step 3: Get Build Definitions From Team Project

Using the IBuildServer method called = QueryBuildDefinitions to get all build definitions be Team Project Name.

private void ListProjectsSelectionChanged(object sender, SelectionChangedEventArgs e)
{     
if (listProjects.SelectedItem != null)  
  {    
    listBuildDefs.ItemsSource = _buildServer.QueryBuildDefinitions(((Project) listProjects.SelectedItem).Name);
    } }
Than using QueryBuildControllers to get the Build Controller assign to a specific Build Definition.
private void ListBuildDefsSelectionChanged(object sender, SelectionChangedEventArgs e)
{     
if (listBuildDefs.SelectedItem != null)  
  {        
//True to Include Agents for the Controller. 
       listControllers.ItemsSource =      
      _buildServer.QueryBuildControllers(true).Where(  
              b => b.Name.Equals(((IBuildDefinition) listBuildDefs.SelectedItem).BuildController.Name)); 
   } }
And Based on the Build Controller you can get all Agents under the Build Controller.
private void ListControllersSelectionChanged(object sender, SelectionChangedEventArgs e)
{     
if (listControllers.SelectedItem != null)
    {  
      listAgents.ItemsSource = ((IBuildController) listControllers.SelectedItem).Agents; 
   } }
And last thing is to Get the controller values.(The reason all lines are comment is the values are assigned from the XAML).
private void ListTfsControllersSelectionChanged(object sender, SelectionChangedEventArgs e)
{     
if (listTfsControllers.SelectedItem != null
   {      
  var controller = (IBuildController) listTfsControllers.SelectedItem;
       //txtPort            => controller.ServiceHost.BaseUrl.Port;  
     //txtMachineName     => controller.ServiceHost.BaseUrl.Host; 
      //txtControllerName  => controller.Name;     
   //txtServiceHostName => controller.ServiceHost.Name;  
  } }

Step 4: Add New Build Controller

private void BtnAddControllerClick(object sender, RoutedEventArgs e)
{    
try  
  { 
       //Creates a new service host with the specified name and base URL. 
       var serviceHost = _buildServer.CreateBuildServiceHost(txtMachineName.Text, new Uri("http://" + txtServiceHostName.Text + ":" + txtPort.Text + "/"));
       serviceHost.Save();   
     //Creates a build controller that is associated with the current service host. 
       var controller = serviceHost.CreateBuildController(txtControllerName.Text);     
    controller.Save();   
     listTfsControllers.ItemsSource = _buildServer.QueryBuildControllers(); 
   }    
catch (BuildServiceHostAlreadyExistsException alreadyExistsException)
   {   
     MessageBox.Show(alreadyExistsException.Message);
   }
}

Step 5: Delete Build Controller

private void BtnRemoveClick(object sender, RoutedEventArgs e)
{    
if (listTfsControllers.SelectedItem != null)  
  {       
//Obtain the IBuildController and using IBuildServer calling the DeleteBuildControllers method. 
        var controller = (IBuildController) listTfsControllers.SelectedItem; 
       _buildServer.DeleteBuildControllers(new IBuildController[] {controller});
        listTfsControllers.ItemsSource = _buildServer.QueryBuildControllers();
    } }

Download Demo Project

Add comment
facebook linkedin twitter email

Leave a Reply

one comment

  1. Pete16/05/2011 ב 16:41

    Great article. I came to this late and would like to read starting at part 1. Do you have a index page of all 35 parts?