4

My ultimate goal is to get the parent of one work item at a time recursively, until there are no more parents in the hierarchy. At the moment, there is nothing recursive yet, I am still at the point of optimizing the way I obtain the parent work item. I have thought of a way of doing this involving a query:

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 the linked work items, including predecessor, successor, child and parents. I knew that wiTrees[1] was the parent work item so I hard coded 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 do I go from here?

Jean-François Beaulieu
  • 4,305
  • 22
  • 74
  • 107

2 Answers2

12

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);
Michael Logutov
  • 2,551
  • 4
  • 28
  • 32
  • I was able to use this snippet and fit it into my code and get it working in thirty seconds. Nice. – JohnZaj Dec 14 '14 at 22:07
7

Found a solution, which returns the parent WorkItem if there is a parent, if not returns 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;
    }
Jean-François Beaulieu
  • 4,305
  • 22
  • 74
  • 107
  • 1
    you can extend your query using the mode(mustcontain) to fetch only queries with the specified link type, that way you know for sure you're retrieving the right ones: `SELECT [System.Id] FROM WorkItemLinks WHERE ([Source].[System.WorkItemType] = 'User Story') And ([System.Links.LinkType] = 'Parent')) mode(MustContain)` (See: http://msdn.microsoft.com/en-us/library/bb130306.aspx#sectionToggle2) – jessehouwing Feb 20 '12 at 22:33