Reputation: 789
How to query linked Workitems in TFS java sdk. Below is the code for querying normal workitems from tfs:
public static void main(final String[] args) {
final TFSTeamProjectCollection tpc = SnippetSettings.connectToTFS();
final Project project = tpc.getWorkItemClient().getProjects().get(SnippetSettings.PROJECT_NAME);
final WorkItemClient workItemClient = project.getWorkItemClient();
// Define the WIQL query.
final String wiqlQuery = "Select ID, Title from WorkItems where (State = 'Active') order by Title"; //$NON-NLS-1$
// Run the query and get the results.
final WorkItemCollection workItems = workItemClient.query(wiqlQuery);
System.out.println("Found " + workItems.size() + " work items."); //$NON-NLS-1$ //$NON-NLS-2$
System.out.println();
// Write out the heading.
System.out.println("Query: " + wiqlQuery); //$NON-NLS-1$
System.out.println();
System.out.println("ID\tTitle"); //$NON-NLS-1$
// Output the results of the query.
final int maxToPrint = 20;
for (int i = 0; i < workItems.size(); i++) {
if (i >= maxToPrint) {
System.out.println("[...]"); //$NON-NLS-1$
break;
}
final WorkItem workItem = workItems.getWorkItem(i);
System.out.println(workItem.getID() + "\t" + workItem.getTitle()); //$NON-NLS-1$
}
}
While running the linked work item query using the above code I am getting an error saying:
com.microsoft.tfs.core.clients.workitem.exceptions.ValidationException: TF248021: You have specified a query string that is not valid when you use the query method for a flat list of work items. You cannot specify a parameterized query or a query string for linked work items with the query method you specified.
Upvotes: 0
Views: 688
Reputation: 31023
When you execute RunQuery() on a flat query, you'll get a successful response. But if you execute RunQuery() on linked query (tree or one-hop WIQL), you’ll see this error message.
To execute the linked query, you must use the RunLinkQuery() method instead of RunQuery() on the Query class. This returns an array of WorkItemLinkInfo objects, which contains the following fields: SourceId, TargetId, LinkTypeId and IsLocked.
Below is an example of a tree query:
SELECT [System.Id], [System.State], [System.WorkItemType]
FROM WorkItemLinks
WHERE
(
Source.[System.TeamProject] = 'TFSTestProject'
AND Source.[System.WorkItemType] = 'Requirement'
AND Source.[System.State] NOT IN ('Proposed', 'Completed')
)
AND [System.Links.LinkType] = 'System.LinkTypes.Hierarchy-Forward'
AND Target.[System.WorkItemType] <> ''
ORDER BY [System.Id] mode(Recursive)
And below is an example of executing a tree query in c#:
private List<WorkItemViewModel> GetWorkItemTree(string query)
{
var treeQuery = new Query(_workItemStore, query);
var links = treeQuery.RunLinkQuery();
var workItemIds = links.Select(l => l.TargetId).ToArray();
query = "SELECT * FROM WorkItems";
var flatQuery = new Query(_workItemStore, query, workItemIds);
var workItemCollection = flatQuery.RunQuery();
var workItemList = new List<WorkItemViewModel>();
for (int i = 0; i < workItemCollection.Count; i++)
{
var workItem = workItemCollection[i];
if (workItem.Type.Name == "Requirement")
{
var model = new WorkItemViewModel()
{
Id = workItem.Id,
RequestNo = workItem.Fields["MyCompany.RequestNumber"].Value.ToString(),
Title = workItem.Title,
WorkItemType = workItem.Fields["MyCompany.WorkItemType"].Value.ToString(),
Priority = workItem.Fields["MyCompany.Priority"].Value.ToString()
};
workItemList.Add(model);
}
else
{
switch (workItem.Type.Name)
{
case "Task":
var task = new TFSTask()
{
Name = workItem.Title,
Activity = workItem.Fields["MyCompany.Activity"].Value.ToString(),
Start = (DateTime?)workItem.Fields["MyCompany.ActivityStart"].Value,
Due = (DateTime?)workItem.Fields["MyCompany.ActivityFinish"].Value,
Status = workItem.State
};
workItemList.Last().Tasks.Add(task);
break;
case "Issue":
var issue = new TFSIssue()
{
Name = workItem.Title,
Created = workItem.CreatedDate,
Due = (DateTime?)workItem.Fields["MyCompany.ActivityDue"].Value,
Status = workItem.State
};
workItemList.Last().Issues.Add(issue);
break;
default:
break;
}
}
}
return workItemList;
}
Useful links:
By the way, you can also use TFS Rest api to run a linked query:
https://learn.microsoft.com/en-us/rest/api/vsts/wit/wiql/query%20by%20wiql
Upvotes: 1