如何:向 SharePoint 项目项扩展中添加属性

可以使用项目项扩展向 Visual Studio 中已安装的任何 SharePoint 项目项添加属性。 当在**“解决方案资源管理器”中选择项目项时,该属性将出现在“属性”**窗口中。

下面的步骤假定您已创建了一个项目项扩展。 有关更多信息,请参见如何:创建 SharePoint 项目项扩展

向项目项扩展中添加属性

  1. 定义包含一个公共属性的类,该公共属性表示要添加到项目项类型的属性。 如果要将多个属性添加到一个项目项类型,则可以在同一个类中定义所有属性,也可以在不同的类中定义所有属性。

  2. ISharePointProjectItemTypeExtension 实现的 Initialize 方法中,处理 projectItemType 参数的 ProjectItemPropertiesRequested 事件。

  3. ProjectItemPropertiesRequested 事件的事件处理程序中,将您的属性类的实例添加到事件实参参数的 PropertySources 集合中。

示例

下面的代码示例演示如何将一个名为**“Example Property”(示例属性)**的属性添加到“事件接收器”项目项。

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

Namespace Contoso.Examples.ProjectItemExtensionWithProperty

    <Export(GetType(ISharePointProjectItemTypeExtension))> _
    <SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")> _
    Friend Class ExampleProjectItemExtensionWithProperty
        Implements ISharePointProjectItemTypeExtension

        Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
            Implements ISharePointProjectItemTypeExtension.Initialize
            AddHandler projectItemType.ProjectItemPropertiesRequested, AddressOf ProjectItemPropertiesRequested
        End Sub

        Private Sub ProjectItemPropertiesRequested(ByVal Sender As Object,
            ByVal e As SharePointProjectItemPropertiesRequestedEventArgs)
            Dim propertyObject As CustomProperties = Nothing

            ' If the properties object already exists, get it from the project item's annotations.
            If False = e.ProjectItem.Annotations.TryGetValue(propertyObject) Then
                ' Otherwise, create a new properties object and add it to the annotations.
                propertyObject = New CustomProperties(e.ProjectItem)
                e.ProjectItem.Annotations.Add(propertyObject)
            End If
            e.PropertySources.Add(propertyObject)
        End Sub
    End Class

    Friend Class CustomProperties
        Private projectItem As ISharePointProjectItem

        Friend Sub New(ByVal projectItem As ISharePointProjectItem)
            Me.projectItem = projectItem
        End Sub

        Private Const TestPropertyId As String = "Contoso.ExampleProperty"
        Private Const PropertyDefaultValue As String = "This is an example property."

        <DisplayName("Example Property")> _
        <DescriptionAttribute("This is an example property for project items.")> _
        <DefaultValue(PropertyDefaultValue)> _
        Public Property ExampleProperty As String
            Get
                Dim propertyValue As String = Nothing

                ' Get the current property value if it already exists; otherwise, return a default value.
                If False = projectItem.ExtensionData.TryGetValue(TestPropertyId, propertyValue) Then
                    propertyValue = PropertyDefaultValue
                End If
                Return propertyValue
            End Get
            Set(ByVal value As String)
                If value <> PropertyDefaultValue Then
                    ' 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(TestPropertyId) = value
                Else
                    ' Do not save the default value.
                    projectItem.ExtensionData.Remove(TestPropertyId)
                End If
            End Set
        End Property
    End Class
End Namespace
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);
                }
            }
        }
    }
}

了解代码

为了确保每当 ProjectItemPropertiesRequested 事件发生时都使用 CustomProperties 类的同一个实例,该代码示例在此事件第一次发生时,将属性对象添加到项目项的 Annotations 属性。 无论此事件什么时候再次发生,代码都将检索此对象。 有关使用 Annotations 属性将数据与项目项关联的更多信息,请参见将自定义数据与 SharePoint 工具扩展相关联

为了保持对属性值的更改,ExampleProperty 的 set 访问器会将新值保存到与该属性关联的 ISharePointProjectItem 对象的 ExtensionData 属性中。 有关使用 ExtensionData 属性将数据与项目项保存在一起的更多信息,请参见在 SharePoint 项目系统的扩展中保存数据

指定自定义属性的行为

通过将 System.ComponentModel 命名空间中的特性应用于属性定义,可定义自定义属性在**“属性”**窗口中的显示方式和行为方式。 下列特性可用于多种方案:

编译代码

这些示例需要引用了下列程序集的类库项目:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

部署扩展

若要部署扩展,请为要随此扩展分发的程序集和任何其他文件创建 Visual Studio 扩展 (VSIX) 包。 有关更多信息,请参见在 Visual Studio 中部署 SharePoint 工具扩展

请参见

任务

如何:创建 SharePoint 项目项扩展

演练:扩展 SharePoint 项目项类型

其他资源

如何:向 SharePoint 项目项扩展中添加快捷菜单项

扩展 SharePoint 项目项