Condividi tramite


F# and TFS2010–Retrieving all Work Items

 

Today’s post is merely a little code fragment, that gets you started with using the TFS2010 API from F#.

Assuming you have referenced the following Assemblies:

  1. System.Windows.Forms
  2. Microsoft.TeamFoundation.Client
  3. Microsoft.TeamFoundation.Common
  4. Microsoft.TeamFoundation.WorkItemTracking.Client

the following code connects to a TFS Server (cbtfs, you might want to change the URI to your needs) on the default port (8080) and loads up information about the TeamProjectCollection DefaultCollection. This will succeed under the assumption that valid credentials are supplied to the UICredentialsProvider. The UICredentialsProvider is also the reason why the assembly reference to System.Windows.Forms is demanded.

After successful authentication, a simple work item query (wiql) is constructed retrieving all work items available for that TeamProjectCollection. Finally, each work item’s id and title information is displayed on the console.

Edit 2011-06-02

I corrected the flow to account for previously checked project collection exception and incorporated a use of sequence expressions to make it feel more F#-ish

 

 // Learn more about F# at https://fsharp.net
module TFSTest

open System.Net
open Microsoft.TeamFoundation
open Microsoft.TeamFoundation.Common
open Microsoft.TeamFoundation.Client
open Microsoft.TeamFoundation.WorkItemTracking.Client

let projectCollectionUri = System.Uri("https://cbtfs:8080/tfs/DefaultCollection")
let mutable projectCollection:TfsTeamProjectCollection = null

try
    
    projectCollection <- TfsTeamProjectCollectionFactory.GetTeamProjectCollection(
        projectCollectionUri
        , new UICredentialsProvider()
    )
    
    projectCollection.EnsureAuthenticated()

    printfn "authenticated with your tfs ... "
with
    | :? TeamFoundationServerUnauthorizedException -> 
        ( printfn "error: failed to authorize with TFS service!")
    | :? TeamFoundationServiceUnavailableException -> 
        ( printfn "error: failed to connect to TFS service - service unavailable!")
    | :? WebException -> 
        (printfn "error: Network problem!" )

let mutable workItemStoreService = null

if (projectCollection <> null) then
    try
        workItemStoreService <- projectCollection.GetService<WorkItemStore>()
        printfn "Connected to WorkItemStoreService"
    with
        | :? System.Exception -> 
            (printfn "error: failed to connect to workitemstore")

if (workItemStoreService <> null) then 

    let workItems = workItemStoreService.Query(
        "SELECT [System.Id], [System.Title] FROM WorkItems"
    )

    if (workItems <> null && workItems.Count > 0) then
        let workItemSequence = seq { for i in 1 .. (workItems.Count - 1) do yield workItems.Item(i) }
        workItemSequence 
            |> Seq.iter(fun item -> (printfn "[%d] %s" (item.Id) (item.Title)))
 
printfn "press any key to exit"
System.Console.ReadLine() |> ignore