Search code examples
c#azure-devopsazure-devops-rest-apiworkitem

System.Description not present in the dictionary


I'm writing a C# program that can get the fields of all the work items in an Azure DevOps organization. However, I'm having some problems with the Description field:

IList<WorkItem> workItems = new List<WorkItem>();
            string uri = "https://dev.azure.com/(organization)";
            var creds = new VssBasicCredential(string.Empty, "pat");

            var wiql = new Wiql()
            {
                Query = "SELECT [Id] FROM WorkItems"
            };

            using (var httpClient = new WorkItemTrackingHttpClient(new Uri(uri), creds))
            {
                var result = httpClient.QueryByWiqlAsync(wiql).Result;
                var ids = result.WorkItems.Select(item => item.Id).ToArray();

                if (ids.Length == 0)
                { 
                    workItems = Array.Empty<WorkItem>();
                    return;
                }

                var fields = new[] 
                { 
                    "System.Title", 
                    "System.WorkItemType", 
                    "System.State", 
                    "System.TeamProject",
                    "System.Description"
                };

                workItems = httpClient.GetWorkItemsAsync(ids, fields, result.AsOf).Result;
            }

            Console.WriteLine("{0} Items Found: ", workItems.Count);
            Console.WriteLine();
            Console.WriteLine("ID\tTitle\tType\tState\tProject\tDescription");
            foreach (WorkItem item in workItems)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", 
                    item.Id, 
                    item.Fields["System.Title"], 
                    item.Fields["System.WorkItemType"], 
                    item.Fields["System.State"], 
                    item.Fields["System.TeamProject"],
                    item.Fields["System.Description"]);
            }

Here is also the project references:

using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;

This code is supposed to return all the parameters, but it gives the following exception:

"The given key 'System.Description' was not present in the dictionary."

Is there any way to solve this?


Solution

  • I think I found a solution: you basically have to make sure that the WI description is not null and, if it is, you want to show a custom message saying that there is no description to the WI:

    foreach (WorkItem item in workItems)
    {
        string desc = item.Fields["System.Description"].ToString();
        if (desc == string.Empty) desc = "(missing description)";
        Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", 
            item.Id,
            item.Fields["System.Title"],
            item.Fields["System.WorkItemType"],
            item.Fields["System.State"],
            item.Fields["System.TeamProject"],
            desc);
    }
    

    This way it will show you the description.