如何从TFS获取构建定义?

时间:2013-04-19 10:07:30

标签: c# tfs tfs2012 alm

如何使用collection-guidproject-name

的TFS-API获取构建定义

有关详细信息,请参阅here。这是关于TFS的最佳资源之一。

1 个答案:

答案 0 :(得分:9)

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;
}