VSTS work items list through REST API

匿名 (未验证) 提交于 2019-12-03 01:36:02

问题:

How can I get a list of work items from VSTS using the REST API?

According to the documentation, the ids parameter is optional, but when I omit it I get a 404 error. If I add the ids parameter, I can get the items.

Failing request:
GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?api-version=1.0

Succeeding request:
GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?ids=252&api-version=1.0

Authentication is the same for both.

The complete problem to solve is: get all features in a specific VSTS project

回答1:

The key is to use WIQL part of the API, not the Work Item one. For example to get a flat list of Work Items of some type use this: https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql#a-flat-query

Example in PowerShell (shows all User Stories in Closed state):

# using env vars passed from VSTS build $collectionuri = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI $token = $Env:SYSTEM_ACCESSTOKEN # need to configure build to allow passing OAuth tokens  $basicAuth = "{0}:{1}"-f "ivan-the-terrible", $token $basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth) $basicAuth = [System.Convert]::ToBase64String($basicAuth) $headers = @{Authorization=("Basic {0}"-f $basicAuth)}  $WorkItemType = 'User Story'  $url = $collectionuri + 'DefaultCollection/_apis/wit/wiql?api-version=1.0'  $WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = '" + $WorkItemType + "' AND [State] = 'Closed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc" $body = @{ query = $WIQL_query } $bodyJson=@($body) | ConvertTo-Json  $response = Invoke-RestMethod -Uri $url -headers $headers -Method Post -ContentType "application/json" -Body $bodyJson  $workitems = $response.workItems  Write-Host "Found" $workitems.Count "work items of type:" $WorkItemType 


回答2:

Here is a similar solution that I wrote in C# to return a list of all work items of type Ticket. I was able to build this result utilizing the sample provided in the accepted answer and looking at the docs here

public QueryResult GetTickets() {     try     {         var token = "****";          using (var client = new HttpClient())         {             client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",                 Convert.ToBase64String(                     System.Text.ASCIIEncoding.ASCII.GetBytes(                         string.Format("{0}:{1}", "", token))));              var query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Ticket' AND [State] <> 'Closed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate]";                                 var content = new StringContent("{ \"query\": \"" + query + "\" }", Encoding.UTF8, "application/json");             var url = "https://{account}.visualstudio.com/_apis/wit/wiql?api-version=4.1";              using (HttpResponseMessage response = client.PostAsync(url, content).Result)             {                 response.EnsureSuccessStatusCode();                 string responseBody = response.Content.ReadAsStringAsync().Result;                 var result = JsonConvert.DeserializeObject<QueryResult>(responseBody);                  return result;             }         }     }     catch(Exception ex)     {         return null;     }             } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!