Freigeben über


TFS 2010 - Adding a menu item to the Completed Builds context menu in the Build Explorer

So, I have gotten this question a couple of times... "How can I add my own custom menu item to a TFS Build Automation context menu?" Yes, this is possible and not really that difficult. We even added some extensibility in 2010 to make this an even better experience. So, here are the steps that I followed and some tips along the way.

First you need a Visual Studio package (I think an addin will work too, but I haven’t tried that). 

  • I created a c# package by using the VS Package wizard from the New Project dialog
  • I selected to include a Menu Command and skipped most other things
  • Running the project launches a new VS and shows my menu item on the tools menu

Then, I changed the code to query for the IVsTeamFoundationBuild interface that the Build package provides.

  • First you have to add a reference for the assembly “Microsoft.VisualStudio.TeamFoundation.Build”. I directly edited the csproj file, but you should be able to do it from the add reference dialog.
  • Then I added code to the Package class in the MenuItemCallback method like this:

        private void MenuItemCallback(object sender, EventArgs e)

        {

            String message = null;

            IVsTeamFoundationBuild vsTfBuild = (IVsTeamFoundationBuild)GetService(typeof(IVsTeamFoundationBuild));

            if (vsTfBuild != null)

            {

                message = "got it!";

            }

            else

            {

                message = "Unable to get vsTfBuild.";

            }

            // Show a Message Box to prove we were here

            IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));

            Guid clsid = Guid.Empty;

            int result;

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(

                       0,

                       ref clsid,

                       "BuildIntegrationPackage",

                       message,

                       string.Empty,

                       0,

                       OLEMSGBUTTON.OLEMSGBUTTON_OK,

                       OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,

                       OLEMSGICON.OLEMSGICON_INFO,

                       0,        // false

                       out result));

        }

 

Next, I changed the vsct file that defines the menu to move it to the Build Explorer context menu. 

  • So, first you have to know the Guid and id of the menu where you want your menu group to show up. The two Build Explorer menus have the following guid/id pairs
    • Completed Builds
      • 34586048-8400-472e-BBBF-3AE30AF8046E
      • 0x105
    • Queued Builds
      • 34586048-8400-472e-BBBF-3AE30AF8046E
      • 0x108
  • I put my menu group on the Completed Builds menu (at the bottom)
  • Here is what that looks like in the VSCT file (this is the new Groups section):

    <Groups>

      <Group guid="guidBuildIntegrationPackageCmdSet" id="MyMenuGroup" priority="0x0600">

        <Parent guid="guidCompletedBuilds" id="menuCompletedBuilds"/>

      </Group>

    </Groups>

  • The parent guid and id names are defined in the Symbols section near the bottom:

    <!-- This is the Build Explorer Menu. -->

    <GuidSymbol name="guidCompletedBuilds" value="{34586048-8400-472e-BBBF-3AE30AF8046E}" >

      <IDSymbol name="menuCompletedBuilds" value="0x105"/>

    </GuidSymbol>

  • That should put your menu item group on the right context menu.

Then, all you have to do is write the code in your menu handler.

  • This requires you to add a reference to the assemblies Microsoft.TeamFoundation.Build.Client and Microsoft.TeamFoundation.Build.Common.
  • Here is the code to get the selected build:

            String message = null;

            IVsTeamFoundationBuild vsTfBuild = (IVsTeamFoundationBuild)GetService(typeof(IVsTeamFoundationBuild));

            if (vsTfBuild != null)

            {

                IBuildDetail[] builds = vsTfBuild.BuildExplorer.CompletedView.SelectedBuilds;

                if (builds.Length == 1)

                {

                    message = "Got Build: " + builds[0].BuildNumber;

                }

                else

                {

                    message = "Got " + builds.Length.ToString() + " Builds";

                }

            }

            else

            {

                message = "Unable to get vsTfBuild.";

            }

After doing all of that, I have a menu group at the bottom of the Completed Builds context menu that shows a message with the build number or number of builds that are selected. I didn’t cover deployment of the VS package here. You will have to look that up, but it should be pretty straight forward and well documented.

Happy coding!

Comments

  • Anonymous
    May 02, 2011
    Was this at all possible in VSTS 2008? We won't get to 2010 until next year. :-(

  • Anonymous
    June 01, 2011
    Actually, it is possible in 2008 as well. I don't think there was an easy way to get the selected items, but you can certainly add a menu item to the same menus. The Guids and ids have not changes since 2008.

  • Anonymous
    July 10, 2012
    I am just wondering, where to get the complete GUIDs and IDs?

  • Anonymous
    July 10, 2012
    The one's for Build can be found here: blogs.msdn.com/.../tfs-2010-build-menu-ids-and-guids.aspx

  • Anonymous
    August 18, 2012
    Hi, I am able to get the menu comes for every build definition context menu. But how to get the context of the build definition when I click the menu. Like edit build definition opens a window of referred build definition. Thanks, Sundar

  • Anonymous
    August 19, 2012
    Hi Sundar, If your context menu is showing up when you right click a build in Build Explorer (as in this example), you can get to the definition information from the build object. IBuildDetail[] builds = vsTfBuild.BuildExplorer.CompletedView.SelectedBuilds; IBuildDefinition definition0 = builds[0].BuildDefinition; Hope that helps, Jason

  • Anonymous
    August 19, 2012
    Hi Jason, This doesn’t work for me. The reason is I have added context menu in Build Definition. At that time I do not have build explorer open. So vsTfBuild.BuildExplorer.CompletedView.SelectedBuilds is null. I am trying to get context of build definition when I right click the build definition and click the newly added menu. It is similar to Edit Build Definition menu. Thanks, Sundar

  • Anonymous
    August 28, 2012
    Hi Sundar,   What version of Visual Studio are you using? If you are on VS 2012, you can easy get the selected definition through the Team Explorer extensibility. If you are on VS 2008 or 2010, you should be able to get the selected object in Team Explorer by using the Visual Studio IVsSelectionContext service. I am not really sure if this will have everything you want. Hope that points you in the right direction, Jason

  • Anonymous
    September 10, 2012
    Can u tell me where is GetService Method defined?

  • Anonymous
    September 11, 2012
    Hi Mukesh, In the example above, the GetService method is part of the base Package class that Visual Studio provides. If you create your Package using the wizard you should get this method. If you already have a package, you can simply use whatever object you have that can get Visual Studio services. Our service is a standard VS service and so can be queried like any other. Hope that helps, Jason

  • Anonymous
    November 10, 2014
    Thanks for this informative article :)

  • Anonymous
    November 19, 2014
    Nice article, Mukesh, I'm trying to use your code on VS 2012 / Windows Server 2012 R2, but cannot find "Microsoft.VisualStudio.TeamFoundation.Build" reference to add to my project, so the IVsTeamFoundationBuild is not recognized by intellisense. What should I do? Thanks in advance

  • Anonymous
    November 19, 2014
    Sorry, meant VS 2013. :)

  • Anonymous
    November 19, 2014
    That assembly is part of Visual Studio. try searching for it from the VS root under Program Files (x86). It has moved around a few times, but it is probably under common7ideprivateAssemblies Note there is a simpler way to customize the build report that works for VS and for the Web using the WriteCustomSummarySection activity. It doesn't give you the full power of this extensibility point, but it is much easier to use. Good Luck, Jason