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
In the Initialize method of your ISharePointProjectExtension implementation, handle the ProjectMenuItemsRequested event of the projectService parameter.
In your event handler for the ProjectMenuItemsRequested event, add a new IMenuItem object to the ActionMenuItems or AddMenuItems collection of the event arguments parameter.
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 Using the SharePoint Project Service.
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
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);
}
}
}
}
Compiling the Code
This example requires a class library project with references to the following assemblies:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
Deploying 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 Deploying Extensions for the SharePoint Tools in Visual Studio.