如何以编程方式更新自定义TFS字段

时间:2011-02-07 15:13:20

标签: tfs tfs-sdk

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

2 个答案:

答案 0 :(得分:13)

这样的事情对你有用:

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应该是字段的名称,而不是refname(即 Integration Build ,而不是 Microsoft.VSTS.Build.IntegrationBuild

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

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

答案 1 :(得分:4)

TFS 2012已经改变了,基本上你必须添加工作Item.Fields [要更新的字段] .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();    
}