tfstfs-sdk

TFS | Get Linked User Story to workitem using c#


I want to fetch user story which is linked to a test case. I have test case details but want to fetch the linked user story through it in c#.

Not able to find any helpful reference online.


Solution

  • You could try this code snipped below:

    WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();
    var workItem = workItemStore.GetWorkItem(testCaseId);
    List<WorkItem> wilist = new List<WorkItem>();
    foreach (WorkItemLink wiLink in workItem.WorkItemLinks)
    {
        if (wiLink.LinkTypeEnd.Name.Equals("Tests"))
        {
                        int targetId = wiLink.TargetId;
                        var linkedWI = workItemStore.GetWorkItem(targetId);
                        if (linkedWI.Fields["Work Item Type"].Value.ToString() == "User Story")
                        {
                            wilist.Add(linkedWI);   // Add the user story to the list                        
                        }
        }
    }
    

    You could also use this TFS REST API:

    GET  http://serverName:8080/tfs/DefaultCollection/_apis/wit/workitems/309?$expand=relations&api-version=1.0
    

    About using it in C#, please refer to: https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#with-links-and-attachments-1