How to get build definitions from TFS?

Martin picture Martin · Apr 19, 2013 · Viewed 7.5k times · Source

How to get build definitions using TFS-API given collection-guid and project-name

Please refer here for more details. This is one of the best resources on TFS stuff.

Answer

Martin picture Martin · Apr 19, 2013
private TfsConfigurationServer configurationServer;  

configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(uri);



public IList<KeyValuePair<string, Uri>> GetBuildDefinitionListFromProject(Guid collectionId, string projectName)
{
    List<IBuildDefinition> buildDefinitionList = null;
    List<KeyValuePair<string, Uri>> buildDefinitionInfoList = null;

    try
    {
        buildDefinitionInfoList = new List<KeyValuePair<string, Uri>>();
        TfsTeamProjectCollection tfsProjectCollection =
        configurationServer.GetTeamProjectCollection(collectionId);
        tfsProjectCollection.Authenticate();
        var buildServer = (IBuildServer)tfsProjectCollection.GetService(typeof(IBuildServer));
        buildDefinitionList = new List<IBuildDefinition>(buildServer.QueryBuildDefinitions(projectName));
    }
    catch (Exception e)
    {
        ApplicationLogger.Log(e);
    }

    if (buildDefinitionList != null && buildDefinitionList.Count > 0)
    {
        foreach (IBuildDefinition builddef in buildDefinitionList)
        {
            buildDefinitionInfoList.Add(new KeyValuePair<string, Uri>(builddef.Name, builddef.Uri));
        }
    }
    return buildDefinitionInfoList;
}