如何在TFS java sdk中查询链接的工作项。 以下是从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$
}
}
使用上面的代码运行链接的工作项查询时,我收到错误消息:
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.
答案 0 :(得分:1)
当您在平面查询上执行RunQuery()时,您将获得成功的响应。但是,如果您在链接查询(树或一跳 WIQL)上执行RunQuery(),您将看到此错误消息。
要执行链接查询,必须在Query类上使用RunLinkQuery() method而不是RunQuery()。这将返回一个WorkItemLinkInfo对象数组,其中包含以下字段:SourceId,TargetId,LinkTypeId和IsLocked。
以下是树查询的示例:
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)
以下是在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;
}
有用的链接:
顺便说一句,您还可以使用TFS Rest api来运行链接查询:
https://docs.microsoft.com/en-us/rest/api/vsts/wit/wiql/query%20by%20wiql