如何:向自定义 SharePoint 项目项类型中添加属性

在定义自定义 SharePoint 项目项类型时,可向项目项中添加属性。 当在**“解决方案资源管理器”中选择项目项时,该属性将出现在“属性”**窗口中。

下面的步骤假定您已定义了自己的 SharePoint 项目项类型。 有关更多信息,请参见如何:定义 SharePoint 项目项类型

向项目项类型定义添加属性

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

  2. ISharePointProjectItemTypeProvider 实现的 InitializeType 方法中,处理 projectItemTypeDefinition 参数的 ProjectItemPropertiesRequested 事件。

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

示例

下面的代码示例演示如何将一个名为**“Example Property”**的属性添加到自定义项目项类型。

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

Namespace Contoso.Examples.ProjectItemTypeWithProperty

    <Export(GetType(ISharePointProjectItemTypeProvider))> _
    <SharePointProjectItemType("Contoso.ExampleProjectItemType")> _
    <SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")> _
    Friend Class ExampleProjectItemTypeWithProperty
        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.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.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);
                }
            }
        }
    }
}

了解代码

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

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

指定自定义属性的行为

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

编译代码

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

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

部署项目项

若要使其他开发人员能够使用您的项目项,请创建一个项目模板或项目项模板。 有关更多信息,请参见为 SharePoint 项目项创建项模板和项目模板

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

请参见

任务

如何:定义 SharePoint 项目项类型

其他资源

如何:向自定义 SharePoint 项目项类型中添加快捷菜单项

定义自定义 SharePoint 项目项类型