Logo
HOWTO: Get the Pending Changes in the Team Explorer of Visual Studio from a package

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2012
Date: April 2015   Microsoft Visual Studio 2013
      Microsoft Visual Studio 2015
Introduction

The Team Explorer of Visual Studio 2012 and higher is composed of pages (such as Home, My Work, Pending Changes, Work Items, Builds, etc.) and in turn each page is composed of sections. For example, the Pending Changes page is composed of the Policy Warnings, Included Changes, Excluded Changes, Comment and Related Work Items sections. While an extension (such as a package) could interact directly as a client with some TFS server or Git Repository, in some cases your extension may be interested in the information of the different pages and sections of the Team Explorer client, for example to provide new pages, new sections inside an existing page, new menu entries in the context menus of the Team Explorer, etc.

More Information

The following package creates a menu command that, when clicked, shows the information about the Pending Changes page. The required assemblies are the following:

  • Microsoft.TeamFoundation.Controls.dll (in folder C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ReferenceAssemblies\v4.5\)
  • Microsoft.TeamFoundation.VersionControl.Client.dll (in folder C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ReferenceAssemblies\v2.0\)
  • Microsoft.TeamFoundation.VersionControl.Controls.dll (in folder C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\PrivateAssemblies\)
  • Microsoft.TeamFoundation.WorkItemTracking.Client.dll (in folder C:\Program Files (x86)\Microsoft Visual Studio <version>\Common7\IDE\ReferenceAssemblies\v2.0\)
Language: C#   Copy Code Copy Code (IE only)
using System;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using Microsoft.VisualStudio.Shell;

namespace Company.VSPackageTeamExplorer
{
   [PackageRegistration(UseManagedResourcesOnly = true)]
   [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
   [ProvideMenuResource("Menus.ctmenu", 1)]
   [Guid(GuidList.guidVSPackageTeamExplorerPkgString)]
   public sealed partial class VSPackageTeamExplorerPackage : Package
   {
      public VSPackageTeamExplorerPackage()
      {
      }

      protected override void Initialize()
      {
         base.Initialize();

         // Add our command handlers for menu (commands must exist in the .vsct file)
         OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
         if (null != mcs)
         {
            // Create the command for the menu item.
            CommandID menuCommandID = new CommandID(GuidList.guidVSPackageTeamExplorerCmdSet, (int)PkgCmdIDList.cmdidTeamExplorerInfo);
            MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
            mcs.AddCommand(menuItem);
         }
      }

      private void MenuItemCallback(object sender, EventArgs e)
      {
         Microsoft.TeamFoundation.Controls.ITeamExplorer teamExplorer;

         teamExplorer = base.GetService(typeof(Microsoft.TeamFoundation.Controls.ITeamExplorer))
            as Microsoft.TeamFoundation.Controls.ITeamExplorer;

         ShowPendingChangesPageInformation(teamExplorer, Microsoft.TeamFoundation.Controls.TeamExplorerPageIds.PendingChanges);
      }

      private void ShowPendingChangesPageInformation(Microsoft.TeamFoundation.Controls.ITeamExplorer teamExplorer, string pageGuid)
      {
         Microsoft.TeamFoundation.Controls.ITeamExplorerPage teamExplorerPage;
         Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.IPendingChangesExt pendingChangesExt;

         teamExplorerPage = teamExplorer.NavigateToPage(new Guid(pageGuid), null);

         pendingChangesExt = teamExplorerPage.GetExtensibilityService(
            typeof(Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.IPendingChangesExt))
            as Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.IPendingChangesExt;

         if (pendingChangesExt != null)
         {
            ShowPendingChangesProperties(pendingChangesExt);

            ShowPendingChanges("ExcludedChanges", pendingChangesExt.ExcludedChanges);

            ShowPendingChanges("FilteredExcludedChanges", pendingChangesExt.FilteredExcludedChanges);

            ShowPendingChanges("IncludedChanges", pendingChangesExt.IncludedChanges);

            ShowPendingChanges("FilteredIncludedChanges", pendingChangesExt.FilteredIncludedChanges);

            ShowPendingChangesItems("SelectedExcludedItems", pendingChangesExt.SelectedExcludedItems);

            ShowPendingChangesItems("SelectedIncludedItems", pendingChangesExt.SelectedIncludedItems);

            ShowPolicyFailures("PolicyFailures", pendingChangesExt.PolicyFailures);

            ShowPolicyFailures("SelectedPolicyFailures", pendingChangesExt.SelectedPolicyFailures);

            ShowWorkItemCheckingInfos("WorkItems", pendingChangesExt.WorkItems);

            ShowWorkItemCheckingInfos("SelectedWorkItems", pendingChangesExt.SelectedWorkItems);
         }
      }

      private void ShowPendingChangesProperties(
         Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.IPendingChangesExt pendingChangesExt)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine("CheckinComment: " + pendingChangesExt.CheckinComment);
         stringBuilder.AppendLine("CheckinNote: " + pendingChangesExt.CheckinNote);
         stringBuilder.AppendLine("ExcludedChangesFilterText: " + pendingChangesExt.ExcludedChangesFilterText);
         stringBuilder.AppendLine("FilterBy: " + pendingChangesExt.FilterBy);
         stringBuilder.AppendLine("IncludedChangesFilterText: " + pendingChangesExt.IncludedChangesFilterText);
         stringBuilder.AppendLine("PolicyOverrideComment: " + pendingChangesExt.PolicyOverrideComment);
         stringBuilder.AppendLine("Workspace: " + pendingChangesExt.Workspace);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }

      private void ShowPendingChanges(string description, Microsoft.TeamFoundation.VersionControl.Client.PendingChange[] pendingChanges)
      {
         foreach (Microsoft.TeamFoundation.VersionControl.Client.PendingChange pendingChange in pendingChanges)
         {
            ShowPendingChange(description, pendingChange);
         }
      }

      private void ShowPendingChange(string description, Microsoft.TeamFoundation.VersionControl.Client.PendingChange pendingChange)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("FileName: " + pendingChange.FileName);

         stringBuilder.AppendLine("ChangeType: " + pendingChange.ChangeType);
         stringBuilder.AppendLine("ChangeTypeName: " + pendingChange.ChangeTypeName);
         stringBuilder.AppendLine("ConflictType: " + pendingChange.ConflictType);
         stringBuilder.AppendLine("CreationDate: " + pendingChange.CreationDate);
         stringBuilder.AppendLine("DeletionId: " + pendingChange.DeletionId);
         stringBuilder.AppendLine("Encoding: " + pendingChange.Encoding);
         stringBuilder.AppendLine("EncodingName: " + pendingChange.EncodingName);
         stringBuilder.AppendLine("IsAdd: " + pendingChange.IsAdd);
         stringBuilder.AppendLine("IsBranch: " + pendingChange.IsBranch);
         stringBuilder.AppendLine("IsCandidate: " + pendingChange.IsCandidate);
         stringBuilder.AppendLine("IsDelete: " + pendingChange.IsDelete);
         stringBuilder.AppendLine("IsEdit: " + pendingChange.IsEdit);
         stringBuilder.AppendLine("IsEncoding: " + pendingChange.IsEncoding);
         stringBuilder.AppendLine("IsImplicit: " + pendingChange.IsImplicit);
         stringBuilder.AppendLine("IsLocalItemDelete: " + pendingChange.IsLocalItemDelete);
         stringBuilder.AppendLine("IsLock: " + pendingChange.IsLock);
         stringBuilder.AppendLine("IsMerge: " + pendingChange.IsMerge);
         stringBuilder.AppendLine("IsRename: " + pendingChange.IsRename);
         stringBuilder.AppendLine("IsRollback: " + pendingChange.IsRollback);
         stringBuilder.AppendLine("IsUndelete: " + pendingChange.IsUndelete);
         stringBuilder.AppendLine("IsUnshelvedChange: " + pendingChange.IsUnshelvedChange);
         stringBuilder.AppendLine("ItemId: " + pendingChange.ItemId);
         stringBuilder.AppendLine("ItemType: " + pendingChange.ItemType);
         stringBuilder.AppendLine("Length: " + pendingChange.Length);
         stringBuilder.AppendLine("LocalItem: " + pendingChange.LocalItem);
         stringBuilder.AppendLine("LocalOrServerFolder: " + pendingChange.LocalOrServerFolder);
         stringBuilder.AppendLine("LocalOrServerItem: " + pendingChange.LocalOrServerItem);
         stringBuilder.AppendLine("LockLevel: " + pendingChange.LockLevel);
         stringBuilder.AppendLine("LockLevelName: " + pendingChange.LockLevelName);
         stringBuilder.AppendLine("LockLevelShortName: " + pendingChange.LockLevelShortName);
         stringBuilder.AppendLine("MergeSources: " + pendingChange.MergeSources);
         stringBuilder.AppendLine("PendingChangeId: " + pendingChange.PendingChangeId);
         //stringBuilder.AppendLine("PropertyValues: " + pendingChange.PropertyValues);
         stringBuilder.AppendLine("ServerItem: " + pendingChange.ServerItem);
         stringBuilder.AppendLine("SourceDeletionId: " + pendingChange.SourceDeletionId);
         stringBuilder.AppendLine("SourceLocalItem: " + pendingChange.SourceLocalItem);
         stringBuilder.AppendLine("SourceServerItem: " + pendingChange.SourceServerItem);
         stringBuilder.AppendLine("SourceVersionFrom: " + pendingChange.SourceVersionFrom);
         stringBuilder.AppendLine("ToolTipText: " + pendingChange.ToolTipText);
         stringBuilder.AppendLine("Undone: " + pendingChange.Undone);
         stringBuilder.AppendLine("UploadHashValue: " + pendingChange.UploadHashValue);
         stringBuilder.AppendLine("Version: " + pendingChange.Version);
         //stringBuilder.AppendLine("VersionControlServer: " + pendingChange.VersionControlServer);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }

      private void ShowPendingChangesItems(string description,
         Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.PendingChangesItem[] pendingChangesItems)
      {
         foreach (Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.PendingChangesItem pendingChangesItem
            in pendingChangesItems)
         {
            ShowPendingChangesItem(description, pendingChangesItem);
         }
      }

      private void ShowPendingChangesItem(string description, 
         Microsoft.TeamFoundation.VersionControl.Controls.Extensibility.PendingChangesItem pendingChangesItem)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("IsPendingChange: " + pendingChangesItem.IsPendingChange);
         stringBuilder.AppendLine("ItemPath: " + pendingChangesItem.ItemPath);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());

         ShowPendingChange(description, pendingChangesItem.PendingChange);
      }

      private void ShowPolicyFailures(string description, Microsoft.TeamFoundation.VersionControl.Client.PolicyFailure[] policyFailures)
      {
         foreach (Microsoft.TeamFoundation.VersionControl.Client.PolicyFailure policyFailure in policyFailures)
         {
            ShowPolicyFailure(description, policyFailure);
         }
      }

      private void ShowPolicyFailure(string description, Microsoft.TeamFoundation.VersionControl.Client.PolicyFailure policyFailure)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("Message: " + policyFailure.Message);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }

      private void ShowWorkItemCheckingInfos(string description,
         Microsoft.TeamFoundation.VersionControl.Client.WorkItemCheckinInfo[] workItemCheckinInfos)
      {
         foreach (Microsoft.TeamFoundation.VersionControl.Client.WorkItemCheckinInfo workItemCheckinInfo in workItemCheckinInfos)
         {
            ShowWorkItemCheckinInfo(description, workItemCheckinInfo);
         }
      }

      private void ShowWorkItemCheckinInfo(string description,
         Microsoft.TeamFoundation.VersionControl.Client.WorkItemCheckinInfo workItemCheckinInfo)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("CheckinAction: " + workItemCheckinInfo.CheckinAction);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());

         ShowWorkItem(description, workItemCheckinInfo.WorkItem);
      }

      private void ShowWorkItem(string description, Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem workItem)
      {
         System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();

         stringBuilder.AppendLine(description + ":");
         stringBuilder.AppendLine();

         stringBuilder.AppendLine("Id: " + workItem.Id);
         stringBuilder.AppendLine("Title: " + workItem.Title);

         stringBuilder.AppendLine("AreaId: " + workItem.AreaId);
         stringBuilder.AppendLine("AreaPath: " + workItem.AreaPath);
         stringBuilder.AppendLine("AttachedFileCount: " + workItem.AttachedFileCount);
         stringBuilder.AppendLine("Attachments: " + workItem.Attachments);
         stringBuilder.AppendLine("AuthorizedDate: " + workItem.AuthorizedDate);
         stringBuilder.AppendLine("ChangedBy: " + workItem.ChangedBy);
         stringBuilder.AppendLine("ChangedDate: " + workItem.ChangedDate);
         stringBuilder.AppendLine("CreatedBy: " + workItem.CreatedBy);
         stringBuilder.AppendLine("CreatedDate: " + workItem.CreatedDate);
         stringBuilder.AppendLine("Description: " + workItem.Description);
         stringBuilder.AppendLine("DisplayForm: " + workItem.DisplayForm);
         stringBuilder.AppendLine("ExternalLinkCount: " + workItem.ExternalLinkCount);
         stringBuilder.AppendLine("Fields: " + workItem.Fields);
         stringBuilder.AppendLine("History: " + workItem.History);
         stringBuilder.AppendLine("HyperLinkCount: " + workItem.HyperLinkCount);
         stringBuilder.AppendLine("IsDirty: " + workItem.IsDirty);
         stringBuilder.AppendLine("IsNew: " + workItem.IsNew);
         stringBuilder.AppendLine("IsOpen: " + workItem.IsOpen);
         stringBuilder.AppendLine("IsPartialOpen: " + workItem.IsPartialOpen);
         stringBuilder.AppendLine("IsReadOnly: " + workItem.IsReadOnly);
         stringBuilder.AppendLine("IsReadOnlyOpen: " + workItem.IsReadOnlyOpen);
         stringBuilder.AppendLine("IterationId: " + workItem.IterationId);
         stringBuilder.AppendLine("IterationPath: " + workItem.IterationPath);
         stringBuilder.AppendLine("Links: " + workItem.Links);
         stringBuilder.AppendLine("NodeName: " + workItem.NodeName);
         stringBuilder.AppendLine("Project: " + workItem.Project);
         stringBuilder.AppendLine("Reason: " + workItem.Reason);
         stringBuilder.AppendLine("RelatedLinkCount: " + workItem.RelatedLinkCount);
         stringBuilder.AppendLine("Rev: " + workItem.Rev);
         stringBuilder.AppendLine("RevisedDate: " + workItem.RevisedDate);
         stringBuilder.AppendLine("Revision: " + workItem.Revision);
         stringBuilder.AppendLine("Revisions: " + workItem.Revisions);
         stringBuilder.AppendLine("State: " + workItem.State);
         //stringBuilder.AppendLine("Store: " + workItem.Store);
         stringBuilder.AppendLine("Tags: " + workItem.Tags);
         stringBuilder.AppendLine("TemporaryId: " + workItem.TemporaryId);
         stringBuilder.AppendLine("Type: " + workItem.Type);
         stringBuilder.AppendLine("Uri: " + workItem.Uri);
         stringBuilder.AppendLine("Watermark: " + workItem.Watermark);
         stringBuilder.AppendLine("WorkItemLinkHistory: " + workItem.WorkItemLinkHistory);
         stringBuilder.AppendLine("WorkItemLinks: " + workItem.WorkItemLinks);

         System.Windows.Forms.MessageBox.Show(stringBuilder.ToString());
      }
   }
}

Related articles



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)