Query TFS work items

07/05/2013 12:17

Use Query to get work items from TFS

 

https://get-powershell.com/post/2008/11/14/PowerShell-and-TFS-Work-Items.aspx
#link to query syntax

https://msdn.microsoft.com/en-us/library/bb130198(v=vs.90).aspx

tfs database field names

https://blogs.msdn.com/b/duat_le/archive/2010/02/25/wiql-for-test.aspx

 

 $ProjectName = "YourProjectName"
 $TfsServerUrl = "https://tfs.yourTfsUrl"
 $AreaPath = "YourProjectName/MyComponentName"
 $WorkItemType = "Task" #Test Case
  
 #load assemblies
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.Client")
    [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.TeamFoundation.WorkItemTracking.Client")

    #get TFS structure, project
    [psobject] $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($TfsServerUrl)
    $WorkItemStore = $tfs.TfsTeamProjectCollection.GetService([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore])
 
 
#Construct query (select all tasks with AreaPath within project with ProjectName) - 'Description HTML' is custom field for some TFS templates
 $query = "
 SELECT [System.Id], [System.WorkItemType], [System.Title], [Microsoft.VSTS.Common.DescriptionHtml]
 FROM WorkItems where [System.TeamProject] = '$ProjectName' 
 AND [System.WorkItemType] = '$WorkItemType'
 AND [System.AreaPath] = '$AreaPath'"
 
 $workItems = $WorkItemStore.Query($query)
   
 $i=1
 foreach($item in $workItems)
 {
     write-host ""
     write-host "-----------"
     write-host "Item " $i
     write-host "-----------"
     $item.Title
  $item.Id
  $item.Fields["Description HTML"].value
    
     $i++
   
 }