【问题标题】:Not able to access the Assigned To field of a Azure DevOps Work Item无法访问 Azure DevOps 工作项的分配给字段
【发布时间】:2020-01-08 01:16:33
【问题描述】:

我正在 Visual Studio 中创建一个控制台应用程序,以从 Azure DevOps 项目中获取工作项详细信息。我无法访问工作项的AssignedTo 字段。

我尝试使用 Microsoft 页面中的代码查询有一些更改的工作项,当我尝试访问 AssignedTo 字段时它显示异常。

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"]);
             }

          }
       }
    }
}

错误是:

System.Collections.Generic.KeyNotFoundException H结果=0x80131577 Message=给定的键不在字典中。 源=mscorlib 堆栈跟踪: 在 System.Collections.Generic.Dictionary`2.get_Item(TKey 键) 在 ScrumBoard.Program.Main(String[] args) 中 C:\Users\Naresh\source\repos\ScrumBoard\ScrumBoard\Program.cs:84 行

【问题讨论】:

  • 你能不能把你的 PAT 屏蔽掉,放到网上给大家看不是个好主意
  • 这是一个无效的 PAT。

标签: c# visual-studio azure-devops azure-devops-rest-api workitem


【解决方案1】:

如果assigned to 字段未分配,则会抛出System.Collections.Generic.KeyNotFoundException 异常。

请检查assigned to字段是否在您查询的工作项中分配。

你的代码没问题,除了我无法编译workItemQueryResult.WorkItems.Count(),我将它转换为 IList。 ((IList&lt;WorkItemReference&gt;)workItemQueryResult.WorkItems).Count()

【讨论】:

  • 是的,在我将分配给字段的工作项从未分配更改后,异常消失了。但是我在控制台输出中得到了一个引用,而不是显示名称或分配了工作项的人。我正在获取 Microsoft.VisualStudio.Services.WebApi.IdentityRef
  • 您需要先将其转换为 JObject。例如:JObject identity = JObject.FromObject(workItem.Fields["System.AssignedTo"]); Console.WriteLine(identity["DisplayName"]);
  • 谢谢你,成功了。如果工作项未分配,我是否可以包含assignto字段
  • 这恐怕只能在代码中完成。下面的代码是例如
  • workItem.Fields.TryGetValue("System.AssignedTo", out Object identityOjbect); if (identityOjbect == null) { identityOjbect = new { DisplayName = "Unassigned" }; } JObject identity = JObject.FromObject(identityOjbect); Console.WriteLine("{0} {1} {2}", workItem.Id, workItem.Fields["System.Title"], identity["DisplayName"]);
【解决方案2】:

这是因为当你得到你指定fields = null的工作项时。

您只需要提供 id 即可,无需任何附加参数:

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

现在您将获得包括System.AssignedTo在内的所有字段。

【讨论】:

  • 仍然会出现同样的异常,如果我删除分配给字段,问题中的上述代码能够打印其他字段
【解决方案3】:

这是新代码:

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();
    }

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2021-03-15
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2020-11-17
    • 2023-04-06
    相关资源
    最近更新 更多