HOW TO:將捷徑功能表項目加入至 SharePoint 專案
您可以將捷徑功能表項目加入至任何 SharePoint 專案。使用者在 [方案總管] 中以滑鼠右鍵按一下專案節點時,就會出現功能表項目。
下列步驟假設您已經建立專案擴充功能。如需詳細資訊,請參閱 HOW TO:建立 SharePoint 專案擴充功能。
若要將捷徑功能表項目加入至 SharePoint 專案
在您的 ISharePointProjectExtension 實作之 Initialize 方法中,請處理 projectService 參數的 ProjectMenuItemsRequested 事件。
在 ProjectMenuItemsRequested 事件的事件處理常式中,將新的 IMenuItem 物件加入至事件引數參數的 ActionMenuItems 或 AddMenuItems 集合。
範例
下列程式碼範例示範如何將捷徑功能表項目加入至 [方案總管] 中的 SharePoint 專案節點。當使用者以滑鼠右鍵按一下專案節點,並且按一下 [將訊息寫入輸出視窗] 功能表項目時,Visual Studio 會在 [輸出] 視窗中顯示訊息。此範例使用 SharePoint 專案服務來顯示訊息。如需詳細資訊,請參閱 使用 SharePoint 專案服務。
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);
}
}
}
}
編譯程式碼
這個範例需要類別庫專案並參考下列組件:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
部署擴充功能
若要部署擴充功能,請針對組件以及要與擴充功能一起散發的任何其他檔案建立 Visual Studio 擴充功能 (VSIX) 套件。如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能。