![](/img/trans.png)
[英]Azure DevOps Rest API - How to grab assigned user's display name in work item
[英]Not able to access the Assigned To field of a Azure DevOps Work Item
我正在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 HResult = 0x80131577 Message =字典中不存在給定的鍵。 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行
這是因為當您獲得工作項時,您指定了fields = null
。
您只需要提供ID,而無需任何其他參數:
var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(arr).Result;
現在,您將獲得所有字段,包括System.AssignedTo
。
這是新的代碼:
靜態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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.