如何:创建 SharePoint 项目项扩展

当您希望向 Visual Studio 中已安装的 SharePoint 项目项添加功能时,请创建项目项扩展。 有关更多信息,请参见扩展 SharePoint 项目项

创建项目项扩展

  1. 创建一个类库项目。

  2. 添加对下列程序集的引用:

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. 创建一个实现 ISharePointProjectItemTypeExtension 接口的类。

  4. 向该类添加下列特性:

  5. Initialize 方法的实现中,使用 projectItemType 参数的成员来定义扩展的行为。 此参数是一个 ISharePointProjectItemType 对象,它提供对 ISharePointProjectItemEventsISharePointProjectItemFileEvents 接口中定义的事件的访问。 若要访问要扩展的项目项类型的特定实例,请处理 ISharePointProjectItemEvents 事件(如 ProjectItemAddedProjectItemInitialized)。

示例

下面的代码示例演示如何为事件接收器项目项创建一个简单的扩展。 每当用户向 SharePoint 项目添加事件接收器项目项时,此扩展就会向**“输出”窗口和“错误列表”**窗口中写入一条消息。

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

Namespace Contoso.ExampleProjectItemExtension

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

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

        Private Sub ProjectItemAdded(ByVal Sender As Object, ByVal e As SharePointProjectItemEventArgs)
            Dim projectItem As ISharePointProjectItem = CType(Sender, ISharePointProjectItem)
            Dim Message As String = String.Format("An Event Handler project item named {0} was added to the {1} project.", _
                projectItem.Name, projectItem.Project.Name)
            projectItem.Project.ProjectService.Logger.WriteLine(Message, LogCategory.Message)
        End Sub
    End Class
End Namespace
using Microsoft.VisualStudio.SharePoint;
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace Contoso.ExampleProjectItemExtension
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")]
    internal class ExampleProjectItemExtension : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemAdded += projectItemType_ProjectItemAdded;
        }

        void projectItemType_ProjectItemAdded(object sender, SharePointProjectItemEventArgs e)
        {
            ISharePointProjectItem projectItem = (ISharePointProjectItem)sender;
            string message = String.Format("An Event Handler project item named {0} was added to the {1} project.",
                projectItem.Name, projectItem.Project.Name);
            projectItem.Project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }
    }
}

此示例使用 SharePoint 项目服务将消息写入到**“输出”窗口和“错误列表”**窗口。 有关更多信息,请参见使用 SharePoint 项目服务

编译代码

此示例需要对以下程序集的引用:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

部署扩展

若要部署扩展,请为要随此扩展分发的程序集和任何其他文件创建 Visual Studio 扩展 (VSIX) 包。 有关更多信息,请参见在 Visual Studio 中部署 SharePoint 工具扩展

请参见

任务

演练:扩展 SharePoint 项目项类型

其他资源

扩展 SharePoint 项目项