【问题标题】:How to update a custom TFS field programmatically如何以编程方式更新自定义 TFS 字段
【发布时间】:2011-06-22 19:30:55
【问题描述】:

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

【问题讨论】:

  • 您能否更具体地了解全局构建列表部分。您是否使用自定义构建模板(在 Windows Workflow Foundation 中)?您是否要添加到该模板中的变量或参数?
  • 对不起,我在 TFS 中使用全局列表。我没有使用构建模板,我们使用的是名为 Automated Build Studio 的产品来进行实际构建本身。我只是要编写一个独立的应用程序来从 ABS 调用这个功能。

标签: tfs tfs-sdk


【解决方案1】:

这样的东西应该适合你:

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

【讨论】:

  • 不确定他们是否更改了某些内容,或者是否每个人都知道这一点,但您需要这样做:workItem.Fields[fieldToUpdate].Value in TFS 2012 否则您将收到有关字段集合的错误只读;
【解决方案2】:

这对于 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();    
}

【讨论】:

    猜你喜欢
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    相关资源
    最近更新 更多