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
Create a class library project.
Add references to the following assemblies:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
Create a class that implements the ISharePointProjectItemTypeExtension interface.
Add the following attributes to the class:
ExportAttribute. This attribute enables Visual Studio to discover and load your ISharePointProjectItemTypeExtension implementation. Pass the ISharePointProjectItemTypeExtension type to the attribute constructor.
SharePointProjectItemTypeAttribute. In a project item extension, this attribute identifies the project item you want to extend. Pass the ID of the project item to the attribute constructor. For a list of the IDs of the project items that are included with Visual Studio, see Extending SharePoint Project Items.
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