2

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?

Community
  • 1
  • 1
Jacob
  • 323
  • 1
  • 7
  • 16

2 Answers2

4

The query results will only contain fields that are non-null, i.e. nobody is assigned to the work item, the field won't be in the Fields dictionary at all.

You need to implement a custom check for those fields and assign them to something according to your logic:

            int[] arr = ids.ToArray();

            string[] fields = new string[] {
                "System.Id", 
                "System.Title",
                "System.State",
                "System.AssignedTo"
            };

            var workItems = await workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields, workItemQueryResult.AsOf);

            List<WorkItemData> list = new List<WorkItemData>();

            foreach (var workItem in workItems)
            {
                var wi = new WorkItemData(workItem.Id.Value);

                wi.Title = workItem.Fields["System.Title"].ToString();
                wi.State = workItem.Fields["System.State"].ToString();
                wi.AssignedTo = workItem.Fields.ContainsKey("System.AssignedTo") ? workItem.Fields["System.AssignedTo"].ToString() : "";

                list.Add(wi);

            }
Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115
2

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)
{
    ......
}
Tingting0929
  • 4,142
  • 1
  • 14
  • 14
  • That worked almost all the way! I was hoping to get the email included in that? How could I do that? I need a unique identifier that distinguishes exactly who the case was assigned to and created by, not just the display name. – Jacob Jan 31 '17 at 22:30