Visual Studio Team Services

Refreshing VisualStudio Work Item ToolWindows programmatically

I recently ran into a situation where I wanted to trigger a refresh and reload of all instances of a particular TFS Work Item programmatically if the user had it open either directly or in a query.

This proved to be non-obvious at first and unfortunately the official MS folks over at the MSDN forums couldn’t really help so I dived a little deeper into the Visual Studio SDK and did in fact find a solution.

First, get ahold of the the Visual Studio TeamFoundation DocumentService Service instance (via your IServiceProvider.GetService(..)).

Secondly, that documentService instance has a method called .GetDocuments() which gets all currently open VisualStudio WorkItem related Documents (=Windows), more particularly instances implementing the IWorkItemTrackingDocument interface.

Thirdly, as the MSDN documentation lacks an overview of classes implementing that interface I had to dig around a little and luckily came across an old, but still valid entry on the MSDN blogs detailing the three document types, namely:

IWorkItemDocument (= the open toolwindows that display a WorkItem) is the easiest one to use at it contains an .Item property which actually is the WorkItem it is displaying, so you can simply go ahead and check whether the particular instance is the one you want to refresh (e.g. by comparing its .Id property) and if so, call the .SyncToLatest() method and you’re done.

Similiarly you can use the IResultsDocument which is basically the split query results view containing a grid with the query results in the one half, and the selected workitem in the other half (a so-called TriageDocument). However, to access the WorkItems the query returned as a result you can either use the (obsolete) .WorkItems property (that was easy) or the now-favored ResultListDataProvider which provides paginated access to the WorkItems. Either way, identify the WorkItem(s) you want to refresh out of these and again, refresh and sync them to their latest state.

And that’s it.


Comments disabled.