0
votes

I am trying to get all tasks from Azure DevOps. I try to follow this documentation: https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/?view=azure-devops-rest-5.0#a-flat-query

I am using this snippet to do requests:

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
        Convert.ToBase64String(
            System.Text.ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "", personalaccesstoken))));

    using (HttpResponseMessage response = await client.GetAsync(url))
    {
        response.EnsureSuccessStatusCode();
        var json = await response.Content.ReadAsStringAsync();

        JToken parsedJson = JToken.Parse(json);
        return parsedJson.ToString(Formatting.Indented);
    }
}

As I understand it you cannot just get all tasks. First step is to get the ID's for all tasks and that needs to be done with a WIQL query. Correct me if I am wrong.

So I try to call following to to get some data for a start: https://dev.azure.com/xxxprod/XXX/XXX Team/_apis/wit/wiql?$top=100&api-version=5.0

However I get a 405 not allowed and I can call other calls like: https://dev.azure.com/xxxprod/_apis/projects

Can anyone provide sample or instruction on how to get all tasks via the REST api?

1

1 Answers

0
votes

As I understand it you cannot just get all tasks. First step is to get the ID's for all tasks and that needs to be done with a WIQL query.

For this issue ,you are right. We could write a WIQL query to fetch the System Ids, then we could according system.Ids to query the workitems.

First you can use work item wiql api to query those items:

POST https://dev.azure.com/{org}/{pro}/_apis/wit/wiql?api-version=5.1

Request body:

{
  "query": "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task'"
}

My result in postman: Result of my query

Then you can use work item list api to list all task work items queried by above step.

Example in powershell scripts:

# query those items

$qurl =  "https://dev.azure.com/{org}/{proj}/_apis/wit/wiql?api-version=5.1"

$WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Task'"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json

$pat = {PAT}

$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))

$result = Invoke-RestMethod -Uri $qurl -Headers @{authorization = "Basic $base64AuthInfo"} -Method post -ContentType "application/json" -Body $bodyJson

# get the work item ids

$ids = $result.workItems | select id | foreach{ $_.id }
$id= '{0}' -f ($ids -join ",")

# use work item list api to list those work items

$url = "https://dev.azure.com/{ORG}/{PROJ}/_apis/wit/workitems?ids=$($id)&api-version=5.1"

$result1 = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method get