简体   繁体   English

无法访问Azure DevOps工作项的“分配给”字段

[英]Not able to access the Assigned To field of a Azure DevOps Work Item

I am creating a console application in visual studio to get the work item details from a Azure DevOps project. 我正在Visual Studio中创建一个控制台应用程序,以从Azure DevOps项目获取工作项详细信息。 I am not able to access the AssignedTo field of a Work item. 我无法访问工作项的AssignedTo字段。

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. 我尝试使用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"]);
             }

          }
       }
    }
}

The error is: 错误是:

System.Collections.Generic.KeyNotFoundException HResult=0x80131577 Message=The given key was not present in the dictionary. System.Collections.Generic.KeyNotFoundException HResult = 0x80131577 Message =字典中不存在给定的键。 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 Source = mscorlib StackTrace:位于System.Collections.Generic.Dictionary`2.get_Item(TKey键)位于ScrumBoard.Program.Main(String [] args)在C:\\ Users \\ Naresh \\ source \\ repos \\ ScrumBoard \\ ScrumBoard \\ Program .cs:第84行

It's because when you get the work items you specify fields = null . 这是因为当您获得工作项时,您指定了fields = null

You need just to give the id's without any additional parameters: 您只需要提供ID,而无需任何其他参数:

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

Now you will get all the fields including System.AssignedTo . 现在,您将获得所有字段,包括System.AssignedTo

This was the new code: 这是新的代码:

static void Main(string[] arg { 静态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();
    }

System.Collections.Generic.KeyNotFoundException exception will be thrown out if the assigned to field is Unassigned. 如果assigned to字段为Unassigned,将抛出System.Collections.Generic.KeyNotFoundException异常。

Please check if the assigned to field is assigned in your queried work item. 请检查assigned to字段是否已在您查询的工作项目中分配。

You code is Ok, Except i cannot compile workItemQueryResult.WorkItems.Count() , I casted it to IList<> instead. 您的代码还可以,除非我无法编译workItemQueryResult.WorkItems.Count() ,但我将其workItemQueryResult.WorkItems.Count()为IList <>。 ((IList<WorkItemReference>)workItemQueryResult.WorkItems).Count()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure DevOps Rest API - 如何在工作项中获取指定用户的显示名称 - Azure DevOps Rest API - How to grab assigned user's display name in work item 如何以编程方式链接 Azure DevOps 中的分支和工作项 - How to link a branch and work item in Azure DevOps programmatically Azure DevOps 使用 API 获取工作项通知 - Azure DevOps Get work item notification using API 使用 Asp.net 更新单个工作项 Azure Devops - Updates a single work item Azure Devops using Asp.net 无法在 Azure Devops 中打开附加到工作项的 Excel 文件 - Unable to open the Excel file attached to work item in Azure Devops Azure DevOps API 创建工作项返回 404 错误 - Azure DevOps API creating work item returns 404 error C# - 如何从 Azure DevOps 工作项列表反序列化 json? - C# - How do I deserialize json from Azure DevOps Work Item List? 使用 C# 通过 API 在 Azure DevOPs 中创建高度自定义的工作项 - Creating a highly customized Work Item in Azure DevOPs through the API using C# 尝试使用Azure DevOps REST API将讨论&#39;评论&#39;发表到工作项时出错 - Error when trying to post a discussion 'comment' to a work item using the Azure DevOps REST API 使用 Azure DevOps 创建工作项 Rest API 使用 ZD7EFA19FBE7D2372FD5ADB6024 - Create Work Item using Azure DevOps Rest API using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM