Bagikan melalui


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.

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

Compiling the Code

This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

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

See Also

Tasks

Walkthrough: Creating a Custom Action Project Item with an Item Template, Part 1

Walkthrough: Creating a Site Column Project Item with a Project Template, Part 1

Concepts

Defining Custom SharePoint Project Item Types

Creating Item Templates and Project Templates for SharePoint Project Items

How to: Add a Property to a Custom SharePoint Project Item Type

How to: Add a Shortcut Menu Item to a Custom SharePoint Project Item Type