c# - 如何通过 API 更改新创建的 TFS 工作项的工作流状态?

标签 c# tfs tfs-sdk

我正在创建一个从“某物”到 TFS 2013 的工作项迁移应用程序,我希望 TFS 工作项处于与源系统中相应的工作流状态。例如,如果源工作项处于“已关闭”状态,我希望它在 TFS 中处于“完成”状态。

我遵循了 this article 中的建议,建议将 WorkItemStore 对象的 BypassRules 属性设置为 true 以便能够设置 CreatedDate 字段.我想,这同样适用于更改工作流状态,因为它也需要绕过规则。

所以,我尝试了以下方法:

// obtain collection and authenticate towards it
var collection = new TfsTeamProjectCollection(new Uri(_tfsUrl), cred);
collection.Authenticate();

// get the work item store object
var store = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);

// creating the work item
var workItem = new WorkItem(store.Projects[_tfsProjectName].WorkItemTypes["Product Backlog Item"]);

// setting some standard fields
workItem.Title = "some name";
workItem.Description = "some description";

// validating the work item
if (workItem.Validate().Count > 0)
{
   // throw validation rules violated
}

// saving the work item
workItem.Save();    

如您所见,此示例未违反任何验证规则,workItem.Validate().Count 返回 0。但是调用 workItem.Save() 会抛出以下异常:

Additional information: TF26212: Team Foundation Server could not save your changes. There may be problems with the work item type definition. Try again or contact your Team Foundation Server administrator.

在调用 Save() 方法之前,我仔细检查了 BypassRules 是否设置为 true。此外,workItem.IsValid 也是 true

有趣的是,如果我改变获取WorkItemStore对象的方式,从

var store = new WorkItemStore(collection, WorkItemStoreFlags.BypassRules);

var store = collection.GetService<WorkItemStore>();

可以毫无问题地保存!但在这种情况下,我不知道如何将 BypassRules 设置为 true。创建 WorkItemStore 对象时此属性是只读的,如果我尝试将工作流步骤设置为“新建”以外的其他内容,则会收到验证错误。

所以,我的基本问题是:如何通过 API 在 TFS 中创建工作项并能够更改这个新创建的项中的 State 字段?

最佳答案

好的,伙计们,正如经常发生的那样,答案在手册中。让我解释一下。

article I referenced在我的问题中明确指出:

You need to be a member of the Project Collection Service Accounts

但它没有提到您不能轻松地将用户或组添加到Project Collection Service Accounts。如果您尝试通过 Web 访问来执行此操作,您将失败 - 添加按钮只是被禁用。此外,屏幕截图具有误导性,将帐户显示为 Project Collection Administrators 组 的成员。

默认情况下,Project Collection Service Accounts 组 包含一个名为Team Foundation Service Accounts 的组。这是您应该将帐户添加到的组。这可以在名为 TFSSecurity.exe 的控制台应用程序的帮助下完成:

TFSSecurity.exe /g+ "Team Foundation Service Accounts" "Domain\my-service-account" /server:http://mytfsserver:8080/tfs

这在 this article 中有详细解释。 ,它以正确的分辨率准确描述了我的情况。 TFSSecurity.exe 可以在以下位置找到: %ProgramFiles(x86)%\Microsoft Visual Studio\Common7\IDE(例如 C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE)

关于c# - 如何通过 API 更改新创建的 TFS 工作项的工作流状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19981287/

相关文章:

c# - 获取最新的 checkin 号(最新的变更集 ID)

C# : concatenate 2 MP3 files

c# - 在 DocumentDB 中查询满足特定条件的文档计数的最佳方法是什么?

android - Android 应用程序的 TFS 自动build设置

visual-studio - 加载解决方案时如何自动切换到 TFS?

tfs - 在仅 64 位的 IIS 进程中使用 TFS2010 客户端

c# - View Model 在构造函数内部时不更新 UI

c# - 将对象设置为 null 是否确定性地处置对象?

javascript - TFS-DASHBOARD - 添加放大按钮小部件

tfs - 如何以编程方式登录到 VSTS