3
votes

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

2
Have you solved this by any chance?Ivan
No, couldn't solve it yetfra
@fra could you please tell me how you have implemented the authentication? How do you get the token and how you send the token with the api for getting the workitems? Thanks in advanceAswin Sathyan

2 Answers

7
votes

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
3
votes

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