Share via


How to: Add a shortcut menu item to a SharePoint project item extension

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can add a shortcut menu item to an existing SharePoint project item by using a project item extension. The menu item appears when a user right-clicks the project item in Solution Explorer.

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

To add a shortcut menu item in a project item extension

  1. In the Initialize method of your ISharePointProjectItemTypeExtension implementation, handle the ProjectItemMenuItemsRequested event of the projectItemType parameter.

  2. In your event handler for the ProjectItemMenuItemsRequested event, add a new IMenuItem object to the ViewMenuItems 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.

Example

The following code example demonstrates how to add a shortcut menu item to the Event Receiver project item. When the user right-clicks the project item in Solution Explorer and clicks the Write Message to Output Window menu item, Visual Studio displays a message in the Output window.

Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint

Namespace Contoso.Examples.ProjectItemExtensionWithMenu

    <Export(GetType(ISharePointProjectItemTypeExtension))> _
    <SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")> _
    Friend Class ExampleProjectItemExtensionWithMenu
        Implements ISharePointProjectItemTypeExtension

        Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
            Implements ISharePointProjectItemTypeExtension.Initialize
            AddHandler projectItemType.ProjectItemMenuItemsRequested, AddressOf ProjectItemMenuItemsRequested
        End Sub

        Private Sub ProjectItemMenuItemsRequested(ByVal Sender As Object,
            ByVal e As SharePointProjectItemMenuItemsRequestedEventArgs)
            Dim menuItem As IMenuItem = e.ViewMenuItems.Add("Write Message to Output Window")
            AddHandler menuItem.Click, AddressOf MenuItem_Click
        End Sub

        Private Sub MenuItem_Click(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
            Dim projectItem As ISharePointProjectItem = CType(e.Owner, ISharePointProjectItem)
            projectItem.Project.ProjectService.Logger.WriteLine(
                String.Format("This message was written from a shortcut menu for {0}.", projectItem.Name),
                LogCategory.Status)
        End Sub
    End Class
End Namespace
using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;

namespace Contoso.Examples.ProjectItemExtensionWithMenu
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")]
    internal class ExampleProjectItemExtensionWithMenu : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemMenuItemsRequested += projectItemType_ProjectItemMenuItemsRequested;
        }

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

        void MenuItemExtension_Click(object sender, MenuItemEventArgs e)
        {
            ISharePointProjectItem projectItem = (ISharePointProjectItem)e.Owner;
            projectItem.Project.ProjectService.Logger.WriteLine(
                String.Format("This message was written from a shortcut menu for {0}.", projectItem.Name), 
                LogCategory.Status);
        }
    }
}

This example uses the SharePoint project service to write the message to the Output window. For more information, see Use the SharePoint project service.

Compile the code

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

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Deploy the extension

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 Deploy extensions for the SharePoint tools in Visual Studio.

See also