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

When you define a custom SharePoint project item type, you can add a property to the project item. The property appears in the Properties window when the project item is selected in Solution Explorer.

The following steps assume that you have already defined your own SharePoint project item type. For more information, see How to: Define a SharePoint Project Item Type.

To add a property to a definition of a project item type

  1. Define a class with a public property that represents the property you are adding to the custom project item type. If you want to add multiple properties to a custom project item type, you can define all the properties in the same class or in different classes.

  2. In the InitializeType method of your ISharePointProjectItemTypeProvider implementation, handle the ProjectItemPropertiesRequested event of the projectItemTypeDefinition parameter.

  3. In the event handler for the ProjectItemPropertiesRequested event, add an instance of your custom properties class to the PropertySources collection of the event arguments parameter.

The following code example demonstrates how to add a property named Example Property to a custom project item type.

using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;

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

            projectItemTypeDefinition.ProjectItemPropertiesRequested += 
                projectItemTypeDefinition_ProjectItemPropertiesRequested;
        }

        void projectItemTypeDefinition_ProjectItemPropertiesRequested(object sender, 
            SharePointProjectItemPropertiesRequestedEventArgs e)
        {
            CustomProperties property;

            // If the properties object already exists, get it from the project item's annotations. 
            if (!e.ProjectItem.Annotations.TryGetValue(out property))
            {
                // Otherwise, create a new properties object and add it to the annotations.
                property = new CustomProperties(e.ProjectItem);
                e.ProjectItem.Annotations.Add(property);
            }

            e.PropertySources.Add(property);
        }
    }

    internal class CustomProperties
    {
        private ISharePointProjectItem projectItem;

        internal CustomProperties(ISharePointProjectItem projectItem)
        {
            this.projectItem = projectItem;
        }

        private const string PropertyId = "Contoso.ExampleProperty";
        private const string PropertyDefaultValue = "This is an example property.";

        [DisplayName("Example Property")]
        [DescriptionAttribute("This is an example property for project items.")]
        [DefaultValue(PropertyDefaultValue)]
        public string ExampleProperty
        {
            get
            {
                string propertyValue;

                // Get the current property value if it already exists; otherwise, return a default value. 
                if (!projectItem.ExtensionData.TryGetValue(PropertyId, out propertyValue))
                {
                    propertyValue = PropertyDefaultValue;
                }
                return propertyValue;
            }
            set
            {
                if (value != PropertyDefaultValue)
                {
                    // Store the property value in the ExtensionData property of the project item.  
                    // Data in the ExtensionData property persists when the project is closed.
                    projectItem.ExtensionData[PropertyId] = value;
                }
                else
                {
                    // Do not save the default value.
                    projectItem.ExtensionData.Remove(PropertyId);
                }
            }
        }
    }
}

To ensure that the same instance of the CustomProperties class is used each time the ProjectItemPropertiesRequested event occurs, the code example saves the properties object to the Annotations property of the project item the first time this event occurs. The code retrieves this object whenever this event occurs again. For more information about using the Annotations property to save data with project items, see Associating Custom Data with SharePoint Tools Extensions.

To persist changes to the property value, the set accessor for ExampleProperty saves the new value to the ExtensionData property of the ISharePointProjectItem object that the property is associated with. For more information about using the ExtensionData property to persist data with project items, see Saving Data in Extensions of the SharePoint Project System.

You can define how a custom property appears and behaves in the Properties window by applying attributes from the System.ComponentModel namespace to the property definition. The following attributes are useful in many scenarios:

  • DisplayNameAttribute : Specifies the name of the property that appears in the Properties window.

  • DescriptionAttribute : Specifies the description string that appears in the bottom of the Properties window when the property is selected.

  • DefaultValueAttribute : Specifies the default value of the property.

  • TypeConverterAttribute : Specifies a custom conversion between the string that is displayed in the Properties window and a non-string property value.

  • EditorAttribute : Specifies a custom editor to use to modify the property.

These code examples require a class library project with 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
Show:
© 2014 Microsoft