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:
System.ComponentModel.Composition.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 2010, 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.
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); } } }
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.
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.