次の方法で共有


How to: Create a SharePoint project item extension

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

Create a project item extension when you want to add functionality to a SharePoint project item that is already installed in Visual Studio. For more information, see Extend SharePoint project items.

To create a project item extension

  1. Create a class library project.

  2. Add references to the following assemblies:

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. Create a class that implements the ISharePointProjectItemTypeExtension interface.

  4. Add the following attributes to the class:

  5. In your implementation of the Initialize method, use members of the projectItemType parameter to define the behavior of your extension. This parameter is an ISharePointProjectItemType object that provides access to the events defined in the ISharePointProjectItemEvents and ISharePointProjectItemFileEvents interfaces. To access a specific instance of the project item type you are extending, handle ISharePointProjectItemEvents events such as ProjectItemAdded and ProjectItemInitialized.

Example

The following code example demonstrates how to create a simple extension for the Event Receiver project item. Each time the user adds an Event Receiver project item to a SharePoint project, this extension writes a message to the Output window and Error List window.

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);
        }
    }
}
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

This example uses the SharePoint project service to write the message to the Output window and Error List window. For more information, see Use the SharePoint project service.

Compile the code

This example requires 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