0

TFS code review comments I am using TFS 2018 On Premise.

Is there any way to get the list of review comments (like shown in Visual studio) using api or using TFS query.

Kuldeep Singh
  • 517
  • 6
  • 26

1 Answers1

0

This case provides a solution, you can check it:

You should be able to get to code review comments with functionality in the Microsoft.TeamFoundation.Discussion.Client namespace.

Specifically the comments are accessible via the DiscussionThread class. And you should be able to query discussions using IDiscussionManager.

Code snippet is as below:

using Microsoft.TeamFoundation.Discussion.Client;
using System;
using System.Collections.Generic;

namespace GetCodeReviewComments
{
   public class ExecuteQuery
    {
        public List<CodeReviewComment> GetCodeReviewComments(int workItemId)
        {
            List<CodeReviewComment> comments = new List<CodeReviewComment>();

            Uri uri = new Uri("http://tfs2018:8080/tfs/defaultcollection");
            TeamFoundationDiscussionService service = new TeamFoundationDiscussionService();
            service.Initialize(new Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(uri));
            IDiscussionManager discussionManager = service.CreateDiscussionManager();

            IAsyncResult result = discussionManager.BeginQueryByCodeReviewRequest(workItemId, QueryStoreOptions.ServerAndLocal, new AsyncCallback(CallCompletedCallback), null);
            var output = discussionManager.EndQueryByCodeReviewRequest(result);

            foreach (DiscussionThread thread in output)
            {
                if (thread.RootComment != null)
                {
                    CodeReviewComment comment = new CodeReviewComment();
                    comment.Author = thread.RootComment.Author.DisplayName;
                    comment.Comment = thread.RootComment.Content;
                    comment.PublishDate = thread.RootComment.PublishedDate.ToShortDateString();
                    comment.ItemName = thread.ItemPath;
                    comments.Add(comment);
                    Console.WriteLine(comment.Comment);
                }
            }

            return comments;
        }

        static void CallCompletedCallback(IAsyncResult result)
        {
            // Handle error conditions here
        }

        public class CodeReviewComment
        {
            public string Author { get; set; }
            public string Comment { get; set; }
            public string PublishDate { get; set; }
            public string ItemName { get; set; }
        }
    }
}
Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Thank you Cece Dong for your response. I have tried this with .net core conole app. Included "Microsoft.TeamFoundationServer.Client" Version="16.153.0" and "Microsoft.TeamFoundationServer.ExtendedClient" Version="16.153.0" but none of them helped in resolving Microsoft.TeamFoundation.Discussion.Client. – Kuldeep Singh May 15 '20 at 08:10
  • You can try a .net framework console app, it would be working. – Cece Dong - MSFT May 15 '20 at 08:13
  • Sure, thank you so much, I will try with .net framework. I was wondering do we have any nuget package for .net core ? – Kuldeep Singh May 15 '20 at 08:15
  • Package "Microsoft.TeamFoundationServer.ExtendedClient" is not compatible well with .net core. Some functions can be using in .net core app, but some can not. So it's recommended using .net framework app instead. – Cece Dong - MSFT May 15 '20 at 08:21
  • Thank you so much @Cece. Appreciate your time. – Kuldeep Singh May 15 '20 at 08:29