0
votes

I have created TFS 2012 Serverside event handler for enforcing code review policy. I am able to create work item for Code review Request and response. But After completed review process, when I am going to check in reviewed code unable to see reviewed work item in Related Work item section in Team explorer window in visual studio 2013. below are my code,

var workItemStore = (WorkItemStore)projectCollection.GetService(typeof(WorkItemStore));
WorkItemType wiType = workItemStore.Projects[0].WorkItemTypes["Code Review Request"];
WorkItem workItem = new WorkItem(wiType);
workItem.Fields["System.AssignedTo"].Value = "XXXXXX"; 
//ev.ChangesetOwner.DisplayName;
workItem.Fields["Microsoft.VSTS.CodeReview.ContextType"].Value = "Shelveset";
workItem.Fields["Microsoft.VSTS.CodeReview.Context"].Value = shelveset.Name;
workItem.Fields["Microsoft.VSTS.CodeReview.ContextOwner"].Value = shelveset.OwnerName;
workItem.Fields["System.AreaPath"].Value = project.Name;
workItem.Fields["System.IterationPath"].Value = project.Name;
workItem.Fields["System.State"].Value = "Requested";
workItem.Fields["System.Reason"].Value = "New";
workItem.Fields["System.Description"].Value = "Code Review Request  ";
workItem.Fields["System.Title"].Value = "Code Review Request " + System.DateTime.Now.ToString();
var invalidFields = workItem.Validate();
if (workItem.IsValid())
  workItem.Save();
var responseId = workItem.Id;
var type = workItemStore.Projects[0].WorkItemTypes["Code Review Response"]; 
workItem = new WorkItem(type);
workItem.Fields["System.AssignedTo"].Value = "xxxxxxxx"; 
workItem.Fields["System.AreaPath"].Value = project.Name; 
workItem.Fields["System.IterationPath"].Value = project.Name;
workItem.Fields["System.State"].Value = "Requested";
workItem.Fields["System.Reason"].Value = "New";
workItem.Fields["Microsoft.VSTS.Common.ReviewedBy"].Value = "xxxxxxxx";
workItem.Fields["System.Title"].Value = "Code Review Response " + System.DateTime.Now.ToString();
WorkItemLinkTypeEnd linkTypeEnd = workItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"];
workItem.Links.Add(new RelatedLink(linkTypeEnd, responseId));
if (workItem.IsValid())
  workItem.Save();
1
Did you mean you want to link a work item to a changeset during the check in?PatrickLu-MSFT
What's the detailed process you want to implement? How do you associate the Code Review request to the code?Eddie Chen - MSFT

1 Answers

0
votes

Seems you didn't link your work item to the related changest. Following code shows how to do this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace APPI
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://xxx.xxx.xxx.xxx:8080/tfs/DefaultCollection";
            TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(url));
            WorkItemStore wis = ttpc.GetService<WorkItemStore>();
            VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
            int wid = 82;
            int cid = 332;
            WorkItem wi = wis.GetWorkItem(wid);
            Changeset cs = vcs.GetChangeset(cid);
            ExternalLink el = new ExternalLink(wis.RegisteredLinkTypes["Fixed in Changeset"], cs.ArtifactUri.AbsoluteUri);
            wi.Links.Add(el);
            wi.Save();     
        }
    }
}

Similar question about TFS - VS Extension: Add work item to pending changes via API and also check this link: C# Programmatically Checking in code changes with TFS API while associating the changeset to a Work Item