uttara bhosale
uttara bhosale

Reputation: 107

Get the workitems of a particular project using TFS api in Powershell

Hi I am trying to get the workitems of a particular project only using below code but i am getting the work item count of all the projects. Please help?

$user = "username"
$token = "PAT"
$count=0
Function QueryWorkItem{

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))


$uri = "http://(url)/tfs/DefaultCollection/(project name)/_apis/wit/wiql?api-version=2.2"
$body = "{
'query':'Select [System.WorkItemType],[System.Title],[System.State],[System.Id] FROM WorkItems '
}"
$result = Invoke-RestMethod -Uri $uri -Method Post -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body

foreach($i in $result.WorkItems){
$result2 = Invoke-RestMethod -Uri https://(uri)/_apis/wit/workItems/$($i.id)?api-version=2.2 -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method Get
foreach ($j in $result2.fields )
{

    if ($j.'System.WorkItemType' -cmatch "Feature")
{

Write-Output ("work item ID: $($i.id) ") |  Out-File -Append Tasks.txt
Write-Output "WorkItem type:" $j.'System.WorkItemType'|  Out-File -Append Tasks.txt
Write-Output "Title:" $j.'System.Title'|  Out-File -Append Tasks.txt
Write-Output "Team Project:" $j.'System.TeamProject'|  Out-File -Append Tasks.txt
Write-Output "--------------------------------------------------------"|  Out-File -Append Tasks.txt

$global:count++
}

}
}

    }

    QueryWorkItem
      Write-Output "count:" $count

Upvotes: 0

Views: 2249

Answers (3)

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31075

The query should looks like:

Select [System.WorkItemType],[System.Title],[System.State],[System.Id] FROM WorkItems WHERE [System.TeamProject] = @project

More information, please refer to the link below:

https://learn.microsoft.com/en-us/vsts/collaborate/wiql-syntax

Upvotes: 1

Daniel Mann
Daniel Mann

Reputation: 59065

Your work item query doesn't filter on System.TeamProject, so it's querying across team projects. Add a WHERE clause to your query.

Upvotes: 0

jessehouwing
jessehouwing

Reputation: 115037

The PowerShell CmdLets offer an extensive list of features that wrap the Client Object model or that call into the REST API directly.

https://github.com/igoravl/tfscmdlets

Example:

# Connect to the FabrikamFiber team project collection
# (Will be used as default for the -Collection argument when required by a cmdlet)
Connect-TfsTeamProjectCollection http://vsalm:8080/tfs/FabrikamFiberCollection

# Get a list of team projects in the currently connected TPC
Get-TfsTeamProject

# List the existing iterations in the FabrikamFiber team project
Get-TfsIteration -Project FabrikamFiber

# Connect to the FabrikamFiber team project
# (will be used as default for the -Project argument when required by a cmdlet)
Connect-TfsTeamProject FabrikamFiber

# Get all bugs in the current team project
Get-TfsWorkItem -Filter '[System.WorkItemType] = "Bug"'

Upvotes: 1

Related Questions