如何:向自定义 SharePoint 项目项类型中添加快捷菜单项
在定义自定义 SharePoint 项目项类型时,可向项目项中添加快捷菜单项。 当用户在**“解决方案资源管理器”**中右击某个项目项时,将会显示相应的菜单项。
下面的步骤假定您已定义了自己的 SharePoint 项目项类型。 有关更多信息,请参见如何:定义 SharePoint 项目项类型。
向自定义项目项类型中添加快捷菜单项
在 ISharePointProjectItemTypeProvider 实现的 InitializeType 方法中,处理 projectItemTypeDefinition 参数的 ProjectItemMenuItemsRequested 事件。
在 ProjectItemMenuItemsRequested 事件的事件处理程序中,将新的 IMenuItem 对象添加到事件实参参数的 ViewMenuItems 或 AddMenuItems 集合中。
示例
下面的代码示例演示如何将上下文菜单项添加到自定义项目项类型中。 当用户在**“解决方案资源管理器”中右击该项目项并单击“Write Message to Output Window”(向输出窗口写入消息)菜单项时,Visual Studio 将在“输出”**窗口中显示一条消息。
Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint
Namespace Contoso.Examples.ProjectItemTypeWithMenu
<Export(GetType(ISharePointProjectItemTypeProvider))> _
<SharePointProjectItemType("Contoso.ExampleProjectItemType")> _
<SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")> _
Friend Class ExampleProjectItemTypeWithMenu
Implements ISharePointProjectItemTypeProvider
Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
Implements ISharePointProjectItemTypeProvider.InitializeType
projectItemTypeDefinition.Name = "ExampleProjectItemType"
projectItemTypeDefinition.SupportedDeploymentScopes = _
SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web
projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All
AddHandler projectItemTypeDefinition.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.ProjectItemTypeWithMenu
{
[Export(typeof(ISharePointProjectItemTypeProvider))]
[SharePointProjectItemType("Contoso.ExampleProjectItemType")]
[SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")]
internal class ExampleProjectItemTypeWithMenu : ISharePointProjectItemTypeProvider
{
public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
{
projectItemTypeDefinition.Name = "ExampleProjectItemType";
projectItemTypeDefinition.SupportedDeploymentScopes =
SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web;
projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;
projectItemTypeDefinition.ProjectItemMenuItemsRequested +=
projectItemTypeDefinition_ProjectItemMenuItemsRequested;
}
void projectItemTypeDefinition_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);
}
}
}
此示例使用 SharePoint 项目服务将消息写入到**“输出”**窗口。 有关更多信息,请参见使用 SharePoint 项目服务。
编译代码
此示例需要引用了下列程序集的类库项目:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
部署项目项
若要使其他开发人员能够使用您的项目项,请创建一个项目模板或项目项模板。 有关更多信息,请参见为 SharePoint 项目项创建项模板和项目模板。
若要部署项目项,请为要随此项目项分发的程序集、模板及任何其他文件创建 Visual Studio 扩展 (VSIX) 包。 有关更多信息,请参见在 Visual Studio 中部署 SharePoint 工具扩展。