0

We use the TFS Java API to fetch WorkItems from a TFS server:

    TFSTeamProjectCollection collection = TFSTeamProjectCollectionUtils
            .openTeamProjectCollection(serverUrl, credentials,
                    new DefaultConnectionAdvisor(Locale.getDefault(),
                            TimeZone.getDefault()));
    WorkItemClient client = collection.getWorkItemClient();

    List<WorkItem> result = new ArrayList<>();
    try {
        WorkItemCollection workItems = client.query(wiqlQuery, null, false);
        for (int i = 0; i < workItems.size(); i++) {
            WorkItem item = workItems.getWorkItem(i);
            result.add(item);
        }
        return result;
    } catch (TECoreException e) {
        throw new ConQATException("Failed to fetch work items from TFS", e);
    }

If I run the query select * from workitems I get all workitems on the server with all fields and all links. Since I'm only interested in some of the fields, I would like to restrict the query to only those and save some bandwidth/time: select ID, Title from workitems

This works fine, but now the links of the items are missing (i.e. item.getLinks() always returns an empty collection).

Is there a way to select the links other than select * from workitems?

Fabian Streitel
  • 2,702
  • 26
  • 36

2 Answers2

0

After some more digging around, I found that you can create a link query and run it like this:

WorkItemLinkInfo[] infos = client.createQuery("select * from workitemlinks").runLinkQuery()

With this, you can get the links as WorkItemLinkInfo objects that contain the IDs of the target and source node and the link type.

Fabian Streitel
  • 2,702
  • 26
  • 36
0

The solution using WorkItemLinkInfo is correct. Just as remark: Using a WIQL Query you only receive the attributes you were querying - which cannot be the set of links of a work item (therefore always empty). If you query a single workitem using

WorkItemClient client = TFSConnection.getClient();
WorkItem firstWorkItem = client.getWorkItemByID(id);

then you also get the LinkCollection using (containing RelatedLinks, ExternalLinks or HyperLinks)

LinkCollection linkcoll = firstWorkItem.getLinks() 
Defarine
  • 293
  • 5
  • 13