1
votes

I am creating a console application in visual studio to get the work item details from a Azure DevOps project. I am not able to access the AssignedTo field of a Work item.

I tried using the code in Microsoft page to query work items with some changes and Its showing a exception when I try to access the AssignedTo field.

static void Main(string[] args)
{
    string _uri = "https://dev.azure.com/xyz";
     string _personalAccessToken = 
     "xpdrix7nyspotj3l4gotvvk4cpp2z6l65g5r";
     string _project = "FirstProject";
     Uri uri = new Uri(_uri);
     string personalAccessToken = _personalAccessToken;
     string project = _project;

     VssBasicCredential credentials = new VssBasicCredential("", 
     _personalAccessToken);

     //create a wiql object and build our query
     Wiql wiql = new Wiql()
     {
         Query = "Select *" +
                 "From WorkItems " +
                 "Where [System.TeamProject] = '" + project + "' " +
                  "Order By [State] Asc, [Changed Date] Desc"
     };

     //create instance of work item tracking http client
      sing (WorkItemTrackingHttpClient workItemTrackingHttpClient = 
      new WorkItemTrackingHttpClient(uri, credentials))
      {
         //execute the query to get the list of work items in the results 
         WorkItemQueryResult workItemQueryResult = 
         workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

         //some error handling                
         if (workItemQueryResult.WorkItems.Count() != 0)
         {
             //need to get the list of our work item id's and put them 
             //into an array
             List<int> list = new List<int>();
             foreach (var item in workItemQueryResult.WorkItems)
             {
                 list.Add(item.Id);
             }
             int[] arr = list.ToArray();

             //build a list of the fields we want to see
             string[] fields = new string[3];
             fields[0] = "System.Id";
             fields[1] = "System.Title";
             fields[2] = "System.AssignedTo";
             WorkItemExpand workItemExpand = WorkItemExpand.All;

             //get work items for the id's found in query
             var workItems = 
             workItemTrackingHttpClient.GetWorkItemsAsync(arr, fields=null, workItemQueryResult.AsOf,workItemExpand).Result;

             Console.WriteLine("Query Results: {0} items found", workItems.Count);

             //loop though work items and write to console
             foreach (var workItem in workItems)
             {
                 Console.WriteLine("{0}{1}{2}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.AssignedTo"]);
             }

          }
       }
    }
}

The error is:

System.Collections.Generic.KeyNotFoundException HResult=0x80131577 Message=The given key was not present in the dictionary. Source=mscorlib StackTrace: at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at ScrumBoard.Program.Main(String[] args) in C:\Users\Naresh\source\repos\ScrumBoard\ScrumBoard\Program.cs:line 84

3
Can you mask your PAT, not a good idea to put it online with everyone to seeR Jain
Thats a invalid PAT.Naresh_drake

3 Answers

1
votes

System.Collections.Generic.KeyNotFoundException exception will be thrown out if the assigned to field is Unassigned.

Please check if the assigned to field is assigned in your queried work item.

You code is Ok, Except i cannot compile workItemQueryResult.WorkItems.Count(), I casted it to IList<> instead. ((IList<WorkItemReference>)workItemQueryResult.WorkItems).Count()

enter image description here

0
votes

It's because when you get the work items you specify fields = null.

You need just to give the id's without any additional parameters:

var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr).Result;

Now you will get all the fields including System.AssignedTo.

0
votes

This was the new code:

static void Main(string[] arg
 {
       
        string _uri = "https://dev.azure.com/xyz";
        string _personalAccessToken = 
       "xpdrix7nyspotj3l4gotvvk4cpp2z6l65g5rd4pfbrl7nskq";
        string _project = "FirstProject";

        /// <summary>
        /// Execute a WIQL query to reutnr a list of bugs using the .NET client library
        /// </summary>
        /// <returns>List of Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem</returns>

        Uri uri = new Uri(_uri);
        string personalAccessToken = _personalAccessToken;
        string project = _project;

        VssBasicCredential credentials = new VssBasicCredential("", _personalAccessToken);

        //create a wiql object and build our query
        Wiql wiql = new Wiql()
        {
            Query = "Select *" +
                    "From WorkItems " +
                    
                    "Where [System.TeamProject] = '" + project + "' " +
                   
                    "Order By [State] Asc, [Changed Date] Desc"
        };

        //create instance of work item tracking http client
        using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
        {
            //execute the query to get the list of work items in teh results
            WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result;

            //some error handling                
            if (workItemQueryResult.WorkItems.Count() != 0)
            {
                //need to get the list of our work item id's and put them into an array
                List<int> list = new List<int>();
                foreach (var item in workItemQueryResult.WorkItems)
                {
                    list.Add(item.Id);
                }
                int[] arr = list.ToArray();

            
                
                

                //get work items for the id's found in query
                var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr).Result;

                Console.WriteLine("Query Results: {0} items found", workItems.Count);

                //loop though work items and write to console
                foreach (var workItem in workItems)
                {
                    Console.WriteLine("{0}          {1}                     {2}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.AssignedTo"]);
                }


            }
        }
        Console.ReadLine();
    }