Search code examples
c#.nettfswiqlazure-devops-rest-api

WIQL Query not including "System.AssignedTo" Field


I have this WIQL, who's purpose is to find the user assigned to the work item.

var wiql = string.Format("SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]" +
                           " FROM WorkItems" +
                           " WHERE ([System.TeamProject] = '{0}')" +
                           " AND ([System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug')" + 
                           " ORDER BY [System.Id]", projectName);

I'm executing it as so...

Wiql wiql = new Wiql() { Query = wiqlQueryString };

  using (var workItemTrackingHttpClient = new WorkItemTrackingHttpClient(VstsAccess.AccessUri, VstsAccess.AccessCredentials))
  {
    var workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

    if (workItemQueryResult != null && workItemQueryResult.WorkItemRelations.Any())
    {
      List<int> sourceIdList = new List<int>();
      foreach (var item in workItemQueryResult.WorkItemRelations)
        sourceIdList.Add(item.Target.Id);

      int[] arr = sourceIdList.ToArray();
      string[] fields = { "System.Id", "System.WorkItemType", "System.AssignedTo", "System.Title", "System.Description", "System.State", "System.IterationPath", "System.TeamProject", "System.ChangedDate", "System.ChangedBy", "System.CreatedDate" };
      return workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf).Result;
    }
    else
      return new List<WorkItem>();
  }

But the "AssignedTo" and "Description" fields are not showing up in the work items' dictionary field-set. Why is this so and how can I fix this?


Solution

  • You could use the code below to query out workitems and it has "AssignedTo" and "Description" field values.

    WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();
    string queryString = string.Format("SELECT [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State]" +
                               " FROM WorkItems" +
                               " WHERE ([System.TeamProject] = '{0}')" +
                               " AND ([System.WorkItemType] = 'Task' OR [System.WorkItemType] = 'Bug')" +
                               " ORDER BY [System.Id]", "Mtt-Scrum"); ;
    
    // Create and run the query.
    Query query = new Query(workItemStore, queryString);
    WorkItemCollection witCollection = query.RunQuery();
    
    foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem in witCollection)
    {
        ......
    }