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