问题
bascially i hierarchy of my TFS is like this
P1
->task1
->task2
->task3
->task4
p1 cantains the following fields
story owner,story author,assigned to,state,priorty,title
and P1 cantains 4 task i want all the details of task with p1 fields(story owner,story author,assigned to,state,priorty and title).
code for this i am not getting p1 fields with it's task details(story owner,story author,assigned to,state,priorty and title).
var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("tfs url"));
WorkItemStore workItemStore = new WorkItemStore(tpc);
Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary<string, string>() { { "project", projectname } });
WorkItemCollection wic = query.RunQuery();
var tpc = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://apactfs.cbre.com:8080/tfs/cbre.apac.applications"));
WorkItemStore workItemStore = new WorkItemStore(tpc);
Query query = new Query(workItemStore, "SELECT * FROM WorkItems WHERE [System.TeamProject] = @project", new Dictionary<string, string>() { { "project", projectname } });
WorkItemCollection wic = query.RunQuery();
foreach (WorkItem item in wic)
{
info += String.Format("{0}\n", item.Title);
}
回答1:
You can get the fields using below query:-
WorkItemCollection wic = query.RunQuery();
foreach (WorkItem item in wic)
{
info += String.Format("{0}\n", item.Title);
var r = item.Fields.OfType<Microsoft.TeamFoundation.WorkItemTracking.Client.Field>()
.Select(x => new
{
Name = x.Name,
Value = x.Value
//You can fetch more details here...
});
}
Please note just for demonstration purpose I have defined a variable here. You should create a custom object and fill it.
Also, if you face any issues related to Field, Hierarchy etc. always debug the code, it will give you clear idea how data is flowing. Based on that you can create your custom object and proceed.
回答2:
You have to get the list of the tasks via one-hop query first and then get the detailed information for it.
TfsTeamProjectCollection TTPC = new TfsTeamProjectCollection(new Uri("http://xxx:8080/tfs/xxxCollection/"));
WorkItemStore wis = TTPC.GetService<WorkItemStore>();
string project = "projectname";
string workitemid = "1";
string wiql = $"SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] FROM WorkItemLinks WHERE (Source.[System.TeamProject] = '{project}' and Source.[System.Id] = {workitemid}) and (Target.[System.TeamProject] = '{project}' and Target.[System.WorkItemType] = 'Task') ORDER BY [System.Id] mode(MustContain)";
Query query = new Query(wis,wiql);
WorkItemLinkInfo[] result = query.RunLinkQuery();
List<WorkItem> tasks = new List<WorkItem> { };
foreach (WorkItemLinkInfo wili in result)
{
if (wili.SourceId == 0)
{
//Get the parent work item here.
}
else
{
//Get the details for the linked tasks and add to tasks list.
tasks.Add(wis.GetWorkItem(wili.TargetId));
}
}
来源:https://stackoverflow.com/questions/48802247/how-to-get-all-fields-details-of-a-workitem-by-wiql-c-sharp-asp-net