Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TFS API - How to obtain the parent of a work item

Tags:

c#

tfs

tfs-sdk

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?

like image 845
JF Beaulieu Avatar asked Dec 02 '22 00:12

JF Beaulieu


1 Answers

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);
like image 115
Michael Logutov Avatar answered Dec 09 '22 13:12

Michael Logutov