TFS API Part 42 – Getting Started With Lab Management API
I did some cool things lately with TFS Lab Management API, from connecting it to TFS Web Access and allow REST access to the lab (if you want to ask why – The customer is always right
).
So this part of TFS API will deal with Lab Management API, although Lab Management API is not public I’ll show some cool things you can do but this will be under your own risk.
Again, this is just the beginning of the Lab Management API, so let’s get started

Download Demo Project
Step 1: Located “Microsoft.TeamFoundation.Lab.Client.dll”
This dll cannot be found under Public, Private or ReferenceAssemblies it can only be found under GAC, so navigate here - C:\Windows\assembly\GAC_MSIL\Microsoft.TeamFoundation.Lab.Client\10.0.0.0__b03f5f7f11d50a3a\
Microsoft.TeamFoundation.Lab.Client.dll
Add this dll as reference to your project.
Step 2: Connect TFS and Obtain LabService
After connecting to TFS you need to obtain the Lab Service at the same way we did for all other services under TFS.
If created a combo box with all team project for later use.
private void BtnConnectClick(object sender, RoutedEventArgs e)
{
var tpp = new TeamProjectPicker(TeamProjectPickerMode.NoProject,false);
tpp.ShowDialog();
if (tpp.SelectedTeamProjectCollection == null) return;
_tfs = tpp.SelectedTeamProjectCollection;
_lab = (LabService)_tfs.GetService(typeof(LabService));
var store = (WorkItemStore)_tfs.GetService<WorkItemStore>();
listProjects.ItemsSource = store.Projects;
}
Step 3: Get Collection Hosts and Library Share
Before we start dealing with Team Projects and their Lab Vm’s and templates the below code will show how to pull the Lab Management Hosts and Library Share from the TFS Collection.
Using the Lab Service and a QuerySpec object (we’ll use those a lot), we need to call QueryTeamProjectCollectionHostGroups method (right now we don’t need to add anything to the Query).
//public class TeamProjectCollectionHostGroupQuerySpec
//{
// public TeamProjectCollectionHostGroupQuerySpec();
// public Uri Location { get; set; }
// public string Name { get; set; }
//}
listHostGroups.ItemsSource = _lab.QueryTeamProjectCollectionHostGroups(
new TeamProjectCollectionHostGroupQuerySpec());
//public class TeamProjectCollectionLibraryShareQuerySpec
//{
// public TeamProjectCollectionLibraryShareQuerySpec();
// public Uri Location { get; set; }
// public string Name { get; set; }
//}
listLibraryShares.ItemsSource = _lab.
QueryTeamProjectCollectionLibraryShares(
new TeamProjectCollectionLibraryShareQuerySpec());
Step 4: Get Lab Management Details For Team Project
Now, when you choose a Team Project we’ll call the GetProjectLabDetails method to extract all relevant lab information on that project.
But first we’ll use the LabService.IsLabConfigured method to see if this project has any lab configurations, if it does then using the QuerySpec we can pull all Environments and Templates for that team project.
void GetProjectLabDetails(string projectName)
{
//Does this Team Project has Lab configure?
if (!_lab.IsLabConfigured(projectName)) return;
//Creating Query for lab environments under a specific project name.
var labQuerySpec = new LabEnvironmentQuerySpec{Project = projectName};
//public class LabEnvironmentQuerySpec
//{
// public LabEnvironmentQuerySpec();
// public LabEnvironmentDisposition Disposition { get; set; }
// public Uri Location { get; set; }
// public string Project { get; set; }
//}
listEnvironments.ItemsSource =_lab.QueryLabEnvironments(labQuerySpec);
listTemplates.ItemsSource = _lab.QueryLabTemplates(new
LabTemplateQuerySpec() { Project = projectName });
}
Download Demo Project
Enjoy.