Search code examples
azure-devopsazure-devops-rest-api

Get all work items from a project for a specific team


The first step will be to get all the work items on "given" project and for a specific team if the project has multiple teams in board. For this step, I was reading azure devops API documentation and found this: Work Items - List

The API is asking for the id of the workitem that I want to get, but what if i need all the workitems from "given" project?

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

I tried this API in postman with query with request body,

{
    "query": "Select [System.WorkItemType] From WorkItems"
}

This is the response i am getting

{
    "queryType": "flat",
    "queryResultType": "workItem",
    "asOf": "2024-05-08T04:14:44.883Z",
    "columns": [
        {
            "referenceName": "System.WorkItemType",
            "name": "Work Item Type",
            "url": "https://dev.azure.com/{org}/_apis/wit/fields/System.WorkItemType"
        }
    ],
    "workItems": [
        {
            "id": 1,
            "url": "https://dev.azure.com/{org}/530d1e3c-9882-4f3a-836c-a7446ad92937/_apis/wit/workItems/1"
        },
        {
            "id": 2,
            "url": "https://dev.azure.com/{org}/530d1e3c-9882-4f3a-836c-a7446ad92937/_apis/wit/workItems/2"
        },
        {
            "id": 3,
            "url": "https://dev.azure.com/{org}/530d1e3c-9882-4f3a-836c-a7446ad92937/_apis/wit/workItems/3"
        },
        {
            "id": 4,
            "url": "https://dev.azure.com/{org}/530d1e3c-9882-4f3a-836c-a7446ad92937/_apis/wit/workItems/4"
        },
        {
            "id": 5,
            "url": "https://dev.azure.com/{org}/530d1e3c-9882-4f3a-836c-a7446ad92937/_apis/wit/workItems/5"
        }
    ]
}

But what I need is, list of work item types available in that project for a specific team. Like Epics, task, feature etc


Solution

  • Based on your requirement, you need to get the all work items from a project for a specific team.

    I am afraid that there is no Out-of-box Rest API can directly get the work items from a specific team.

    There is no field in the work item to directly determine which team it belongs to.

    To meet your requirement, you need to use script to run multiple Rest APIs.

    Here are the Rest API list:

    Wiql - Query By Wiql: Get the work item lists of the organization. Based on my test, the Team argument in the url can not only list the work items in a specific team.

    Teamfieldvalues - Get: Get the team area path value list. We need to use the area path value to determine if the work item belongs to the team.

    Work Items - Get Work Item Get the work item details include area path and work item type.

    Here is the PowerShell script example:

    $token = "PAT"
    $OrganizationName= "OrganizationName"
    $ProjectName = "ProjectName"
    $TeamName = "TeamName"
    
    $url="https://dev.azure.com/$($OrganizationName)/$($ProjectName)/_apis/wit/wiql?api-version=5.1"
    
    $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
    
    $JSON = @'
    {
        "query": "Select [System.WorkItemType] From WorkItems"
    }
    '@
    
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -Body $JSON -ContentType application/json
    
    
    ForEach( $workitemid in $response.workItems.id ) 
    {
    
    
    $url1="https://dev.azure.com/$($OrganizationName)/$($ProjectName)/_apis/wit/workitems/$($workitemid)?fields=System.WorkItemType,System.AreaPath&api-version=6.0"
    
    $responseworkitemdetails = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get 
    
    $workitemtype = $responseworkitemdetails.fields.'System.WorkItemType'
    
    $workitemareapath = $responseworkitemdetails.fields.'System.AreaPath'
    $workitemID = $responseworkitemdetails.id
    
    
    $urlgetteamPath= "https://dev.azure.com/$($OrganizationName)/$($ProjectName)/$($TeamName)/_apis/work/teamsettings/teamfieldvalues?api-version=5.0"
    
    $responsegetteamPath = Invoke-RestMethod -Uri $urlgetteamPath -Headers @{Authorization = "Basic $token"} -Method Get 
    
    foreach($teampath in $responsegetteamPath.values)
    {
      $teampath = $teampath.value
      if($teampath -eq $workitemareapath)
      {
        echo "workitemID: $workitemID"
        echo "workitemtype: $workitemtype"
      }
    
    }
    
    
    }
    

    You can input the required values in the powershell parameters. Then it will return the work items from a specific team and related information.

    Result:

    enter image description here