6

我们有一个自定义构建过程(不使用 MS Build),在此过程中,我将“假”构建添加到全局构建列表中。我这样做的原因是您可以为给定的工作项选择构建(在构建中找到)。我们有一个自定义字段,包含构建,旨在显示该工作项已修复在哪个构建中。我无法弄清楚如何以编程方式更新此字段。我的想法是我将有一个小型应用程序来执行此操作,我将在构建过程中调用它,查找自上次构建以来的所有工作项,然后更新这些工作项的字段。有任何想法吗?

4

2 回答 2

14

像这样的东西应该适合你:

public void UpdateTFSValue(string tfsServerUrl, string fieldToUpdate, 
   string valueToUpdateTo, int workItemID)
{   
    // Connect to the TFS Server
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri));
    // Connect to the store of work items.
    _store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
    // Grab the work item we want to update
    WorkItem workItem = _store.GetWorkItem(workItemId);
    // Open it up for editing.  (Sometimes PartialOpen() works too and takes less time.)
    workItem.Open();
    // Update the field.
    workItem.Fields[fieldToUpdate] = valueToUpdateTo;
    // Save your changes.  If there is a constraint on the field and your value does not 
    // meet it then this save will fail. (Throw an exception.)  I leave that to you to
    // deal with as you see fit.
    workItem.Save();    
}

调用它的一个例子是:

UpdateTFSValue("http://tfs2010dev:8080/tfs", "Integration Build", "Build Name", 1234);

变量fieldToUpdate应该是字段的名称,而不是引用名称(即Integration Build,而不是Microsoft.VSTS.Build.IntegrationBuild

您可能可以使用PartialOpen() 逃脱,但我不确定。

您可能需要添加Microsoft.TeamFoundation.Client到您的项目中。(也许Microsoft.TeamFoundation.Common

于 2011-03-01T21:46:17.520 回答
5

对于 TFS 2012,这已更改,基本上您必须添加 workItem.Fields[fieldToUpdate] .Value

@Vaccano 所写内容的更新版本。

public void UpdateTFSValue(string tfsServerUrl, string fieldToUpdate, 
   string valueToUpdateTo, int workItemID)
{   
    // Connect to the TFS Server
    TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri(tfsUri));
    // Connect to the store of work items.
    _store = (WorkItemStore)tfs.GetService(typeof(WorkItemStore));
    // Grab the work item we want to update
    WorkItem workItem = _store.GetWorkItem(workItemId);
    // Open it up for editing.  (Sometimes PartialOpen() works too and takes less time.)
    workItem.Open();
    // Update the field.
    workItem.Fields[fieldToUpdate].Value = valueToUpdateTo;
    // Save your changes.  If there is a constraint on the field and your value does not 
    // meet it then this save will fail. (Throw an exception.)  I leave that to you to
    // deal with as you see fit.
    workItem.Save();    
}
于 2015-03-16T11:29:48.247 回答