首页 > 解决方案 > Get all work items from devops throug rest api

问题描述

According to documentation you can get work items as following:

GET https://dev.azure.com/{organization}/{project}/_apis/wit/workitems?ids={ids}&api-version=5.1

However you need to provide ids for all the workitems you want. What if you just want all workitems? If I skip Ids I get following:

Response status code does not indicate success: 404 (Not Found).


OK I am getting a little closer thanks to some of the repsonses:

var uri = "https://dev.azure.com/xx"; 
var personalAccessToken = "xx";
var project = "GAC";

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

Wiql wiql = new Wiql()
{
    Query = "Select [State], [Title],[Remaining Work] From WorkItems Where [Work Item Type] = 'Bug' OR [Work Item Type] = 'Task' And [System.TeamProject] = '" + project+"'"
};

//create instance of work item tracking http client
using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(new Uri(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)
    {
        foreach (IEnumerable<WorkItemReference> batch in workItemQueryResult.WorkItems.Batch(100))
        {
            var workItemIds = batch.Select(p => p.Id).ToArray();
            var workItems = workItemTrackingHttpClient.GetWorkItemsAsync(workItemIds, expand: WorkItemExpand.All).Result;
        }                   
    }              
}

However I dont get information around what I am primary looking for like Completed, Remaining works etc. Any pointers?

标签: azure-devopsazure-devops-rest-api

解决方案


you need to create a query to fetch work items programmatically.

check: https://docs.microsoft.com/en-us/azure/devops/integrate/quickstarts/work-item-quickstart?view=azure-devops#create-a-c-project-in-visual-studio

Apart from the core system fields, the fields are very dependent from the process template (Agile, Scrum,...) used. You can even have custom fields. So how to find out the name, or more specific, the ReferenceName, in order to query it?

            using (WorkItemTrackingHttpClient workItemTrackingHttpClient = new WorkItemTrackingHttpClient(uri, credentials))
        {
            List<WorkItemField> fieldsQuery = await workItemTrackingHttpClient.GetFieldsAsync(_project, null, null, System.Threading.CancellationToken.None);

            if (fieldsQuery.Count() != 0)
            {
                foreach (var fieldItem in fieldsQuery)
                {
                    Console.WriteLine("{0} / {1}", fieldItem.Name, fieldItem.ReferenceName);
                }
            }
        }

推荐阅读