34023

VSTS work items list through REST API

Question:

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

According to the <a href="https://www.visualstudio.com/docs/integrate/api/wit/work-items" rel="nofollow" title="documentation">documentation</a>, 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:<br />GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?api-version=1.0

Succeeding request:<br />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: <strong>get all features in a specific VSTS project</strong>

Answer1:

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

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

Answer2:

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 <a href="https://docs.microsoft.com/en-us/rest/api/vsts/wit/wiql/query%20by%20wiql" rel="nofollow">here</a>

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; } }

Recommend

  • Re-enable SSMS Addin
  • 搜索:最优,分支界限
  • 'Localhost' used as Common name in the default WSo2 certificate creates No Subject Alterna
  • How do i uninstall composer on Mac?
  • System.Web.HttpContext.Current nulls itself after checking for a Cache
  • MVC Controller Action only receiving nulls from AJAX
  • Invisible indentation error caused load failure in GHCi [duplicate]
  • Ksql: Left Join Displays columns from stream but not tables
  • Inno Setup create array from comma delimited strings stored in an array
  • How To call Lua function from Javascript
  • Changing FireFox Push Message Provider
  • C func to C++ instance-member trampoline with non-bijective argument type mapping
  • How to retrieve a very big cassandra table and delete some unuse data from it?
  • Hide button if div empty
  • QTableView, setting a cell's font and background colour
  • ruby: how to iterate elements in a hash efficiently
  • Is getTimezoneOffset() stable during daylight saving transition?
  • Mouse clickAndHold() not working properly on Firefox Using Selenium Webdriver
  • load ActiveX object in Applet
  • Draggable nested inside Draggable drags both when dragging child in IE
  • Calling JavaScript function in WebView
  • MySQL database structure for a webshop
  • Retrieve 3rd MAX salary in Hive
  • Isabelle matrix arithmetic: det_linear_row_setsum in library with different notation
  • Working with django : Proxy setup
  • Raw sockets in monotouch
  • SQL Worksheet is not displaying in SQL Developer
  • Oracle REF can't go through
  • Google Apps Script fails to generate image from EmbeddedChartBuilder
  • Set initial vuetify v-select value
  • 2d barcode reader Java ME sdk
  • Keep rows with certain values always at the bottom while sorting in jquery tablesorter plugin
  • Excel File upload in asp.net using SqlBulkCopy
  • Bind selectedDates Aggregation for Calendar
  • Google App Engine backend servlet not responding
  • Computing the discrete fourier transform of audio data with FFTW
  • Using Service Component Runtime
  • convert json to excel in java
  • Create/delete users from text file using Bash script