Partager via


How to: Add a shortcut menu item to SharePoint projects

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 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.

Example

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 Use 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);
            }
        }
    }
}
Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint

Namespace Contoso.Examples

    <Export(GetType(ISharePointProjectExtension))> _
    Friend Class ExampleProjectExtensionWithMenu
        Implements ISharePointProjectExtension

        Private Sub Initialize(ByVal projectService As ISharePointProjectService) _
            Implements ISharePointProjectExtension.Initialize
            AddHandler projectService.ProjectMenuItemsRequested, AddressOf ProjectMenuItemsRequested
        End Sub

        Private Sub ProjectMenuItemsRequested(ByVal Sender As Object,
            ByVal e As SharePointProjectMenuItemsRequestedEventArgs)
            Dim menuItem As IMenuItem = e.ActionMenuItems.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 project As ISharePointProject = TryCast(e.Owner, ISharePointProject)
            If project IsNot Nothing Then
                project.ProjectService.Logger.WriteLine(
                    String.Format("This message was written from a shortcut menu for the {0} project.", project.Name),
                    LogCategory.Status)
            End If
        End Sub
    End Class
End Namespace

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