How to: Add a Shortcut Menu Item to SharePoint Projects

You can add a shortcut menu item to any SharePoint project. The menu item appears when a user right-clicks a project node in Solution Explorer.

The following steps assume that you have already created a project extension. For more information, see How to: Create a SharePoint Project Extension.

To add a shortcut menu item to SharePoint projects

  1. In the Initialize method of your ISharePointProjectExtension implementation, handle the ProjectMenuItemsRequested event of the projectService parameter.

  2. In your event handler for the ProjectMenuItemsRequested event, add a new IMenuItem object to the ActionMenuItems or AddMenuItems collection of the event arguments parameter.

  3. In the Click event handler for the new IMenuItem object, perform the tasks you want to execute when a user clicks your shortcut menu item.

The following code example demonstrates how to add a shortcut menu item to SharePoint project nodes in Solution Explorer. When the user right-clicks a project node and clicks the Write Message to Output Window menu item, Visual Studio displays a message in the Output window. This example uses the SharePoint project service to display the message. For more information, see Using the SharePoint Project Service.

using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;

namespace Contoso.Examples
{
    [Export(typeof(ISharePointProjectExtension))]
    internal class ExampleProjectExtensionWithMenu : ISharePointProjectExtension
    {
        public void Initialize(ISharePointProjectService projectService)
        {
            projectService.ProjectMenuItemsRequested += projectService_ProjectMenuItemsRequested;
        }

        void projectService_ProjectMenuItemsRequested(object sender, 
            SharePointProjectMenuItemsRequestedEventArgs e)
        {
            IMenuItem menuItem = e.ActionMenuItems.Add("Write Message to Output Window");
            menuItem.Click += MenuItemExtension_Click;
        }

        void MenuItemExtension_Click(object sender, MenuItemEventArgs e)
        {
            ISharePointProject project = e.Owner as ISharePointProject;
            if (project != null)
            {
                project.ProjectService.Logger.WriteLine(
                    String.Format("This message was written from a shortcut menu for the {0} project.", project.Name),
                    LogCategory.Status);
            }
        }
    }
}

This example requires a class library project with references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

To deploy the extension, create a Visual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. For more information, see Deploying Extensions for the SharePoint Tools in Visual Studio.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft