Share via


Programmatically Reading Work Item Comments

I recently ran across a few posts from people looking for samples on how to programmatically read user comments from TFS work items using the TFS API.  It’s a simple operation, but it might not be clear at first unless you discover that you need to loop through the collection of Revisions of the work item in order to view those comments.  Below is a quick code sample that demonstrates this behavior.

 // get a reference to the team project collection
using (var prjCol = 
    TfsTeamProjectCollectionFactory.GetTeamProjectCollection(collectionUri))
{
    // get a reference to the work item tracking service
    var workItemStore = prjCol.GetService<WorkItemStore>();

    // grab work item 1460 and print out all associated
    // user comments.
    var workItem = workItemStore.GetWorkItem(1460);
    foreach (Revision r in workItem.Revisions)
    {
        // grab the history field
        Field f = r.Fields["History"];
        Console.WriteLine(f.Name + ": " + f.Value);
    }
}

For more code samples like this check out the Team Foundation Server 2010 SDK on the MSDN Code Gallery.