Condividi tramite


How to: Create a SharePoint Project Item Extension

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 Extending 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.

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

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

Compiling the Code

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

See Also

Tasks

Walkthrough: Extending a SharePoint Project Item Type

Concepts

Extending SharePoint Project Items