Share via


How to: Define a SharePoint project item type

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

Define a project item type when you want to create a custom SharePoint project item. For more information, see Defining custom SharePoint project item types.

To define a project item type

  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 ISharePointProjectItemTypeProvider interface.

  4. Add the following attributes to the class:

    • ExportAttribute. This attribute enables Visual Studio to discover and load your ISharePointProjectItemTypeProvider implementation. Pass the ISharePointProjectItemTypeProvider type to the attribute constructor.

    • SharePointProjectItemTypeAttribute. In a project item type definition, this attribute specifies the string identifier for the new project item. We recommend that you use the format company name.feature name to help make sure that all project items have a unique name.

    • SharePointProjectItemIconAttribute. This attribute specifies the icon to display for this project item in Solution Explorer. This attribute is optional; if you do not apply it to your class, Visual Studio displays a default icon for your project item. If you set this attribute, pass the fully qualified name of an icon or bitmap that is embedded in your assembly.

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

Example

The following code example demonstrates how to define a simple project item type. This project item type writes a message to the Output window and Error List window when a user adds a project item of this type to a project.

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

Namespace Contoso.ExampleProjectItemType

    <Export(GetType(ISharePointProjectItemTypeProvider))> _
    <SharePointProjectItemType("Contoso.ExampleProjectItemType")> _
    <SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")> _
    Friend Class ExampleProjectItemType
        Implements ISharePointProjectItemTypeProvider

        Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
            Implements ISharePointProjectItemTypeProvider.InitializeType
            projectItemTypeDefinition.Name = "ExampleProjectItemType"
            projectItemTypeDefinition.SupportedDeploymentScopes = _
                SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web
            projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All
            AddHandler projectItemTypeDefinition.ProjectItemAdded, AddressOf ProjectItemAdded
        End Sub

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

namespace Contoso.ExampleProjectItemType
{
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("Contoso.ExampleProjectItemType")]
    [SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")]
    internal class ExampleProjectItemType : ISharePointProjectItemTypeProvider
    {
        public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
        {
            projectItemTypeDefinition.Name = "ExampleProjectItemType";
            projectItemTypeDefinition.SupportedDeploymentScopes =
                SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web;
            projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;
            projectItemTypeDefinition.ProjectItemAdded += projectItemTypeDefinition_ProjectItemAdded;
        }

        void projectItemTypeDefinition_ProjectItemAdded(object sender, SharePointProjectItemEventArgs e)
        {
            string message = String.Format("An example project item named {0} was added to the {1} project.",
                e.ProjectItem.Name, e.ProjectItem.Project.Name);
            e.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 Use the SharePoint project service.

Compile the code

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Deploy the project item

To enable other developers to use your project item, create a project template or a project item template. For more information, see Create item templates and project templates for SharePoint project items.

To deploy the project item, create a Visual Studio extension (VSIX) package for the assembly, the template, and any other files that you want to distribute with the project item. For more information, see Deploy Extensions for the SharePoint tools in Visual Studio.

See also