以程序設計方式擷取具有查詢的工作專案
Azure DevOps Services
使用查詢擷取工作專案是 Azure DevOps Services 中的常見案例。 本文說明如何使用 REST API 或 .NET 用戶端連結庫,以程式設計方式實作此案例。
必要條件
Azure DevOps 設定:
- 在 Azure DevOps Services 中擁有 組織 。
- 擁有個人存取令牌(PAT)。
開發環境: 具有 C# 開發環境。 您可以使用 Visual Studio。
在 Visual Studio 中建立 C# 專案
如需 Visual Studio 中 C# 程式設計的相關信息,請參閱 Visual Studio C# 程式設計檔。
C# 程式代碼內容
代碼段中會發生下列工作:
- 驗證
- 使用您的個人存取令牌建立認證(PAT)。
- 使用認證產生用戶端。
- 取得工作專案
- 建立您想要使用的查詢。
- 擷取該查詢的結果。
- 依標識符擷取每個工作專案。
C# 程式碼片段
// nuget:Microsoft.TeamFoundationServer.Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
public class QueryExecutor
{
private readonly Uri uri;
private readonly string personalAccessToken;
/// <summary>
/// Initializes a new instance of the <see cref="QueryExecutor" /> class.
/// </summary>
/// <param name="orgName">
/// An organization in Azure DevOps Services. If you don't have one, you can create one for free:
/// <see href="https://go.microsoft.com/fwlink/?LinkId=307137" />.
/// </param>
/// <param name="personalAccessToken">
/// A Personal Access Token, find out how to create one:
/// <see href="/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops" />.
/// </param>
public QueryExecutor(string orgName, string personalAccessToken)
{
this.uri = new Uri("https://dev.azure.com/" + orgName);
this.personalAccessToken = personalAccessToken;
}
/// <summary>
/// Execute a WIQL (Work Item Query Language) query to return a list of open bugs.
/// </summary>
/// <param name="project">The name of your project within your organization.</param>
/// <returns>A list of <see cref="WorkItem"/> objects representing all the open bugs.</returns>
public async Task<IList<WorkItem>> QueryOpenBugs(string project)
{
var credentials = new VssBasicCredential(string.Empty, this.personalAccessToken);
var wiql = new Wiql()
{
Query = "Select [Id] " +
"From WorkItems " +
"Where [Work Item Type] = 'Bug' " +
"And [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Closed' " +
"Order By [State] Asc, [Changed Date] Desc",
};
using (var httpClient = new WorkItemTrackingHttpClient(this.uri, new VssCredentials(credentials)))
{
try
{
var result = await httpClient.QueryByWiqlAsync(wiql).ConfigureAwait(false);
var ids = result.WorkItems.Select(item => item.Id).ToArray();
if (ids.Length == 0)
{
return Array.Empty<WorkItem>();
}
var fields = new[] { "System.Id", "System.Title", "System.State" };
return await httpClient.GetWorkItemsAsync(ids, fields, result.AsOf).ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine("Error querying work items: " + ex.Message);
return Array.Empty<WorkItem>();
}
}
}
/// <summary>
/// Execute a WIQL (Work Item Query Language) query to print a list of open bugs.
/// </summary>
/// <param name="project">The name of your project within your organization.</param>
/// <returns>An async task.</returns>
public async Task PrintOpenBugsAsync(string project)
{
var workItems = await this.QueryOpenBugs(project).ConfigureAwait(false);
Console.WriteLine("Query Results: {0} items found", workItems.Count);
foreach (var workItem in workItems)
{
Console.WriteLine(
"{0}\t{1}\t{2}",
workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.State"]);
}
}
}