How to: Add a Property to a SharePoint Project Item Extension

You can use a project item extension to add a property to any SharePoint project item that is already installed in Visual Studio. The property appears in the Properties window when the project item is selected in Solution Explorer.

The following steps assume that you have already created a project item extension. For more information, see How to: Create a SharePoint Project Item Extension.

To add a property to a project item extension

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

  2. In the Initialize method of your ISharePointProjectItemTypeExtension implementation, handle the ProjectItemPropertiesRequested event of the projectItemType parameter.

  3. In the event handler for the ProjectItemPropertiesRequested event, add an instance of your 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 the Event Receiver project item.

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

namespace Contoso.Examples.ProjectItemExtensionWithProperty
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")]
    internal class ExampleProjectItemExtensionWithProperty : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemPropertiesRequested += 
                projectItemType_ProjectItemPropertiesRequested;
        }

        void projectItemType_ProjectItemPropertiesRequested(object sender, 
            SharePointProjectItemPropertiesRequestedEventArgs e)
        {
            CustomProperties propertyObject;

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

            e.PropertySources.Add(propertyObject);
        }
    }

    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 adds 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 associate 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 examples require a class library project with references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft