HOW TO:將屬性加入至自訂 SharePoint 專案項目類型
當您定義自訂 SharePoint 專案項目類型時,您可以將屬性加入至專案項目。 當在 [方案總管] 中選取專案項目時,這個屬性就會出現在 [屬性] 視窗中。
下列步驟假設您已經定義自己的 SharePoint 專案項目型別。 如需詳細資訊,請參閱 HOW TO:定義 SharePoint 專案項目類型。
若要將屬性加入至專案項目類型的定義中
定義一個類別,具有表示要加入至自訂專案項目類型的公用屬性。 如果想要將多個屬性加入至自訂專案項目類型,您可以在同一類別或不同類別中定義所有屬性。
在您的 ISharePointProjectItemTypeProvider 實作 InitializeType 方法中,處理 projectItemTypeDefinition 參數的 ProjectItemPropertiesRequested 事件。
在 ProjectItemPropertiesRequested 事件的事件處理常式中,將自訂屬性類別的執行個體加入至事件引數參數的 PropertySources 集合。
範例
下列程式碼範例示範如何將名為 [範例屬性] 的屬性加入至自訂專案項目類型。
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 專案系統擴充功能的資料。
指定自訂屬性的行為
您可以在 [屬性] 視窗中定義自訂屬性 (Property) 的外觀和行為,方法是將屬性 (Attribute) 從 System.ComponentModel 命名空間套用至屬性 (Property) 定義。 下列屬性在許多情節中都十分有用:
DisplayNameAttribute:指定在 [屬性] 視窗中出現的屬性名稱。
DescriptionAttribute:指定當選擇該屬性時,在 [屬性] 視窗底部出現的說明字串。
DefaultValueAttribute:指定屬性的預設值。
TypeConverterAttribute:指定在 [屬性] 視窗中顯示的字串與非字串屬性值之間的自訂轉換。
EditorAttribute:指定用來修改屬性的自訂編輯器。
編譯程式碼
這些程式碼範例需要類別庫專案並參考下列組件:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
部署專案項目
若要讓其他開發人員使用您的專案項目,請建立專案範本或專案項目範本。 如需詳細資訊,請參閱 為 SharePoint 專案項目建立項目範本和專案範本。
若要部署專案項目,請針對組件、範本以及要與專案項目一起散發的任何其他檔案建立 Visual Studio Extension (VSIX) 套件。 如需詳細資訊,請參閱 部署 Visual Studio 中 SharePoint 工具的擴充功能。