Visual Studio Team Services

Opening a TFS WorkItem programmatically from within Visual Studio

If your Visual Studio Add-on or custom WorkItem control also requires the capability to open (other) WorkItems within the VS session, you can use the snippet below:

private static DocumentService _workItemDocumentService;

private static void OpenWorkItem(TeamFoundationServer tfsServer, int workItemId)
  {
    if(_workItemDocumentService == null)
      _workItemDocumentService = (DocumentService)DTE2.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.WorkItemTracking.DocumentService");
      // you can also use _workItemDocumentService = (DocumentService)Package.GetGlobalService(typeof(DocumentService));  

    IWorkItemDocument workItemDocument = workItemDocumentService.GetWorkItem(activeTFS, workItemId, this);
    
    try
    {
      if (!workItemDocument.IsLoaded)
        workItemDocument.Load();

      workItemDocumentService.ShowWorkItem(workItemDocument);
    }
    finally
    {
      workItemDocument.Release(this);
    }
}

Discussion

Comments disabled.