Search code examples
c#tfstfs-sdk

How to get via a query all work items on which someone has worked?


I'm trying to (via wql) get all tasks that have been worked upon, meaning someone logged time for them. For now, I'm doing it in memory:

foreach (WorkItem wi in workItems)
        {
            foreach (Revision item in wi.Revisions)
            {
                try
                {
                     // elided...
                     myTask.TimeSpent = item.Fields["Completed Work"].Value == null ?
                     0 : (double)item.Fields["Completed Work"].Value;
                }
            }
         }

I would like to be able to do it in the initial query like this:

 var query = "SELECT [System.Id] FROM WorkItems
 WHERE [System.TeamProject] = '@project'
 and [System.WorkItemType]='Task' *and [System.WorkItem.Completed_Work] <> 0*";

I tried different ways of drilling into the "completed work" field: [System.WorkItemRevisions.Completed_Work],
[System.WorkItem[Revisions[Completed_Work]]],
[System.WorkItem.Revisions.[Completed Work]]

but I always get an exception. Any ideas?


Solution

  • The problem is the reference name you are using for CompletedWork and another problem would be the @project, because the statement is not called inside a project context and therefor the @project shouldn't work. Here an example how I use it:

    string query = "SELECT [System.Id] FROM WorkItems WHERE [System.TeamProject] = '" + SelectedTeamProject + "' AND [System.WorkItemType]='Task' AND [Microsoft.VSTS.Scheduling.CompletedWork] <> 0";
    

    You can get the right reference name by checking the Work Item Type Definition (WITD) in Visual Studio, if you have the TFS Power Tools installed:

    Tools-> Process Editor-> Work Item Types-> Open WIT from Server
    Choose the Team Project Collection and the TeamProject where the Task is in 
    Open the Task definition and watch out for the Completed Work field and check the reference name for it