Search code examples
tfswiql

How to query Work Item links with specified comment using WIQL


I need a fast way to get a list of links filtered by the link comment. Have a code:

var linkCommentFilter = "Some link comment";
const string queryString = @"SELECT [System.Id]
                               FROM workItemlinks
                               WHERE [System.Links.LinkType] = 'Tested By'
                               AND [System.Links.Comment] = '{0}'
                               AND ([Source].[System.Id] IN ({1}))";

var query = new Query(store, string.Format(queryString, 
    linkCommentFilter,
    string.Join(",", wiIds)));
var result = query.RunLinkQuery().ToArray();

When trying to run this code an exception occurred "field System.Links.Comment doesn't exist". How could I fix it?


Solution

  • You can't query Work Item links with specified comment using WIQL as there is no System.Links.Comment field in TFS.

    You need to use TFS rest api to get a list of work items with links and attachments, check the sample code below:

     public List<WorkItem> GetWorkItemsWithLinksAndAttachments()
            {
                int[] workitemIds = new int[] { 1, 5, 6, 10, 22, 50 };
    
                VssConnection connection = Context.Connection;
                WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
    
                List<WorkItem> workitems = workItemTrackingClient.GetWorkItemsAsync(workitemIds, expand: WorkItemExpand.Links | WorkItemExpand.Relations).Result;
    
                foreach(var workitem in workitems)
                {
                    Console.WriteLine("Work item {0}", workitem.Id);
    
                    foreach (var relation in workitem.Relations)
                    {
                        Console.WriteLine("  {0} {1}", relation.Rel, relation.Url);
                    }
                }
    

    Then find the specified comment with relation.Attributes["comment"] in workitem.Relations.