TFS API - How to get the parent element of a work item

My ultimate goal is to return the parent of one work item recursively, as long as there are no more parents in the hierarchy. There is nothing recursive at the moment, I'm still able to optimize the way I get the parent work item. I thought about how to do this with the request:

public WorkItem GetParentWorkItem(int id) { StringBuilder queryString = new StringBuilder("SELECT [System.Id]" + " FROM WorkItemLinks " + " WHERE [Source].[System.WorkItemType] = '" + TFS_TIMESHEET_WORK_ITEM_TYPE + "'" + " AND [Source].[System.TeamProject] = '" + TFS_TIMESHEET_PROJECT_KEY + "'" + " AND [Source].[System.Id] = " + id ); Query wiQuery = new Query(GetWorkItemStore, queryString.ToString()); WorkItemLinkInfo[] wiTrees = wiQuery.RunLinkQuery(); WorkItem wi = GetWorkItemStore.GetWorkItem(wiTrees[1].TargetId); return wi; } 

The problem with this method is that it gets all related work items, including predecessor, successor, child, and parents. I knew wiTrees[1] is the parent work item, so I hardcoded the index.

I found out a way to get the "parent" WorkItemTypeEnd object from the work item store:

 WorkItemLinkTypeEnd linkTypEnd = GetWorkItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"]; 

Where am I going from here?

+4
source share
2 answers

A solution was found that returns the parent WorkItem if there is a parent if it does not return null.

 public WorkItem GetParentWorkItem(int id) { StringBuilder queryString = new StringBuilder("SELECT [System.Id]" + " FROM WorkItemLinks " + " WHERE [Source].[System.WorkItemType] = '" + TFS_TIMESHEET_WORK_ITEM_TYPE + "'" + " AND [Source].[System.TeamProject] = '" + TFS_TIMESHEET_PROJECT_KEY + "'" + " AND [Source].[System.Id] = " + id ); Query wiQuery = new Query(GetWorkItemStore, queryString.ToString()); WorkItemLinkInfo[] wiTrees = wiQuery.RunLinkQuery(); int parentLinkId = GetWorkItemStore.WorkItemLinkTypes.LinkTypeEnds["Parent"].Id; foreach (WorkItemLinkInfo linkInfo in wiTrees) { // -2 is the LinkTypeId for parent if (linkInfo.LinkTypeId == parentLinkId) { workItem = GetWorkItemStore.GetWorkItem(linkInfo.TargetId); break; } else { workItem = null; } } return workItem; } 
+6
source

This works on TFS 2013:

 var parent_link = work_item.WorkItemLinks.Cast<WorkItemLink> ().FirstOrDefault (x => x.LinkTypeEnd.Name == "Parent"); WorkItem parent_work_item = null; if (parent_link != null) parent_work_item = work_item_store.GetWorkItem (parent_link.TargetId); 
+11
source

Source: https://habr.com/ru/post/1397053/


All Articles