How to: Define a SharePoint Project Item Type

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:

  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.

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.


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 Using the SharePoint Project Service.

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

To enable other developers to use your project item, create a project template or a project item template. For more information, see Creating 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 Deploying Extensions for the SharePoint Tools in Visual Studio.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft