You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过C#调用TFS API实现变更集评审请求?创建工作项后无法评审

How to Implement "Request Review" via TFS API for Changesets

Great question! When you create a code review request through Visual Studio, it handles a few hidden steps that your current code is missing. Let's break down what's wrong and how to fix it:

Key Missing Pieces in Your Code

Visual Studio's "Request Review" creates a pair of linked work items (not just one) and properly associates the changeset with the review. Here's what you're missing:

  • A corresponding Code Review Response work item (this is where reviewers submit their feedback)
  • A formal link between the request and response work items
  • An artifact link connecting the changeset to the review request (not just setting the Associated Context fields)
  • Properly assigning reviewers to the request

Corrected Code Implementation

Here's the full code to replicate the Visual Studio "Request Review" behavior:

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

// Get your TFS team project collection (tpc) instance first
TfsTeamProjectCollection tpc = /* Initialize your TPC connection */;

// 1. Get required services
VersionControlServer vcs = tpc.GetService<VersionControlServer>();
WorkItemStore workItemStore = tpc.GetService<WorkItemStore>();
Project teamProject = workItemStore.Projects["XYZ"];

// 2. Create the Code Review Request work item
WorkItemType requestType = teamProject.WorkItemTypes["Code Review Request"];
WorkItem reviewRequest = new WorkItem(requestType)
{
    Title = $"Review for Changeset 5169",
    AreaPath = @"XYZ\Test",
    IterationPath = @"XYZ\Test\1.5",
    Description = "Please review the changes included in Changeset 5169."
};

// Set associated context (this tells VS what the review is for)
reviewRequest.Fields["Associated Context Type"].Value = "Changeset";
reviewRequest.Fields["Associated Context"].Value = "5169";

// Assign reviewers (adjust field name if your project uses "Code Reviewers" instead)
reviewRequest.Fields["Reviewers"].Value = "Joe Doe";

// 3. Create the paired Code Review Response work item
WorkItemType responseType = teamProject.WorkItemTypes["Code Review Response"];
WorkItem reviewResponse = new WorkItem(responseType)
{
    Title = $"Response: Review for Changeset 5169",
    AreaPath = @"XYZ\Test",
    IterationPath = @"XYZ\Test\1.5",
    Description = "Awaiting reviewer feedback."
};

// 4. Link the response to the request (parent-child relationship)
reviewResponse.Links.Add(new RelatedLink(
    workItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"], 
    reviewRequest.Id
));

// 5. Add a formal artifact link to the changeset
Changeset targetChangeset = vcs.GetChangeset(5169);
reviewRequest.Links.Add(new ExternalLink(
    workItemStore.RegisteredLinkTypes["Fixed in Changeset"], 
    targetChangeset.ArtifactUri.AbsoluteUri
));

// 6. Save both work items (save request first so response can reference its ID)
reviewRequest.Save();
reviewResponse.Save();

Important Notes

  • Field Names May Vary: Depending on your TFS version or custom project configuration, fields like Reviewers might be named Code Reviewers. Double-check your work item type definition to confirm.
  • Permissions: Ensure the account running this code has permissions to create work items, link artifacts, and access the target changeset.
  • Reviewer Assignment: If you need multiple reviewers, the Reviewers field is usually a multi-value field—you can pass a comma-separated list of user names (e.g., "Joe Doe, Jane Smith").

Once you run this code, the review request will behave exactly like the one created in Visual Studio: reviewers will see it in their work items, can access the changeset's file diffs, and submit feedback via the linked response work item.

内容的提问来源于stack exchange,提问作者testalino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:34:53