如何:向 SharePoint 项目中添加属性
可以使用项目扩展向任何 SharePoint 项目中添加属性。 在**“解决方案资源管理器”中选择该属性时,该属性会出现在“属性”**窗口中。
下面的步骤假定您已创建了一个项目扩展。 有关更多信息,请参见如何:创建 SharePoint 项目扩展。
向 SharePoint 项目中添加属性
定义一个具有公共属性的类,该公共属性表示要添加到 SharePoint 项目的属性。 若要添加多个属性,则可以在同一个类中定义所有属性,也可以在不同的类中定义所有属性。
在 ISharePointProjectExtension 实现的 Initialize 方法中,处理 projectService 参数的 ProjectPropertiesRequested 事件。
在 ProjectPropertiesRequested 事件的事件处理程序中,将您的属性类的实例添加到事件实参参数的 PropertySources 集合中。
示例
下面的代码示例演示如何将两个属性添加到 SharePoint 项目中。 其中,一个属性将其数据保留在项目用户选项文件(.csproj.user 文件或 .vbproj.user 文件)中。 另一个属性将其数据保留在项目文件(.csproj 文件或 .vbproj 文件)中保留。
Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio
Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.Shell.Interop
Namespace CustomSharePointProperty
<Export(GetType(ISharePointProjectExtension))> _
Partial Friend Class ProjectExtensionWithProperty
Implements ISharePointProjectExtension
Public Sub Initialize(ByVal projectService As ISharePointProjectService) _
Implements ISharePointProjectExtension.Initialize
AddHandler projectService.ProjectPropertiesRequested, _
AddressOf ProjectPropertiesRequested
End Sub
Private Sub ProjectPropertiesRequested(ByVal sender As Object, _
ByVal e As SharePointProjectPropertiesRequestedEventArgs)
Dim propertiesObject As CustomProjectProperties = Nothing
' If the properties object already exists, get it from the project's annotations.
If False = e.Project.Annotations.TryGetValue(propertiesObject) Then
' Otherwise, create a new properties object and add it to the annotations.
propertiesObject = New CustomProjectProperties(e.Project)
e.Project.Annotations.Add(propertiesObject)
End If
e.PropertySources.Add(propertiesObject)
End Sub
End Class
Public Class CustomProjectProperties
Private sharePointProject As ISharePointProject
Private projectStorage As IVsBuildPropertyStorage
Private Const ProjectFilePropertyId As String = "ContosoCustomProjectFileProperty"
Private Const ProjectFilePropertyDefaultValue As String = "Default"
Public Sub New(ByVal myProject As ISharePointProject)
sharePointProject = myProject
projectStorage = sharePointProject.ProjectService.Convert(Of ISharePointProject, IVsBuildPropertyStorage)(sharePointProject)
End Sub
<DisplayName("Custom Project File Property")> _
<DescriptionAttribute("This property is saved to the .csproj/.vbproj file.")> _
<DefaultValue(ProjectFilePropertyDefaultValue)> _
Public Property CustomProjectFileProperty As String
Get
Dim propertyValue As String = String.Empty
Dim hr As Integer = projectStorage.GetPropertyValue(ProjectFilePropertyId, String.Empty, _
CUInt(_PersistStorageType.PST_PROJECT_FILE), propertyValue)
' Try to get the current value from the project file; if it does not yet exist, return a default value.
If Not ErrorHandler.Succeeded(hr) Or String.IsNullOrEmpty(propertyValue) Then
propertyValue = ProjectFilePropertyDefaultValue
End If
Return propertyValue
End Get
Set(ByVal value As String)
' Do not save the default value.
If value <> ProjectFilePropertyDefaultValue Then
projectStorage.SetPropertyValue(ProjectFilePropertyId, String.Empty, _
CUInt(_PersistStorageType.PST_PROJECT_FILE), value)
End If
End Set
End Property
Private Const UserFilePropertyId As String = "ContosoCustomUserFileProperty"
Private Const UserFilePropertyDefaultValue As String = "Default"
<DisplayName("Custom Project User File Property")> _
<DescriptionAttribute("This property is saved to the .user file.")> _
<DefaultValue(UserFilePropertyDefaultValue)> _
Public Property CustomUserFileProperty As String
Get
Dim propertyValue As String = String.Empty
' Try to get the current value from the .user file; if it does not yet exist, return a default value.
If Not sharePointProject.ProjectUserFileData.TryGetValue(UserFilePropertyId, propertyValue) Then
propertyValue = UserFilePropertyDefaultValue
End If
Return propertyValue
End Get
Set(ByVal value As String)
' Do not save the default value.
If value <> UserFilePropertyDefaultValue Then
sharePointProject.ProjectUserFileData(UserFilePropertyId) = value
End If
End Set
End Property
End Class
End Namespace
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.Shell.Interop;
namespace CustomSharePointProperty
{
[Export(typeof(ISharePointProjectExtension))]
public class ProjectExtensionWithProperty : ISharePointProjectExtension
{
public void Initialize(ISharePointProjectService projectService)
{
projectService.ProjectPropertiesRequested += projectService_ProjectPropertiesRequested;
}
void projectService_ProjectPropertiesRequested(object sender, SharePointProjectPropertiesRequestedEventArgs e)
{
CustomProjectProperties propertiesObject;
// If the properties object already exists, get it from the project's annotations.
if (!e.Project.Annotations.TryGetValue(out propertiesObject))
{
// Otherwise, create a new properties object and add it to the annotations.
propertiesObject = new CustomProjectProperties(e.Project);
e.Project.Annotations.Add(propertiesObject);
}
e.PropertySources.Add(propertiesObject);
}
}
public class CustomProjectProperties
{
private ISharePointProject sharePointProject;
private IVsBuildPropertyStorage projectStorage;
private const string ProjectFilePropertyId = "ContosoCustomProjectFileProperty";
private const string ProjectFilePropertyDefaultValue = "Default";
public CustomProjectProperties(ISharePointProject myProject)
{
sharePointProject = myProject;
projectStorage = sharePointProject.ProjectService.Convert<ISharePointProject, IVsBuildPropertyStorage>(sharePointProject);
}
[DisplayName("Custom Project File Property")]
[DescriptionAttribute("This property is saved to the .csproj/.vbproj file.")]
[DefaultValue(ProjectFilePropertyDefaultValue)]
public string CustomProjectFileProperty
{
get
{
string propertyValue;
int hr = projectStorage.GetPropertyValue(ProjectFilePropertyId, string.Empty,
(uint)_PersistStorageType.PST_PROJECT_FILE, out propertyValue);
// Try to get the current value from the project file; if it does not yet exist, return a default value.
if (!ErrorHandler.Succeeded(hr) || String.IsNullOrEmpty(propertyValue))
{
propertyValue = ProjectFilePropertyDefaultValue;
}
return propertyValue;
}
set
{
// Do not save the default value.
if (value != ProjectFilePropertyDefaultValue)
{
projectStorage.SetPropertyValue(ProjectFilePropertyId, string.Empty,
(uint)_PersistStorageType.PST_PROJECT_FILE, value);
}
}
}
private const string UserFilePropertyId = "ContosoCustomUserFileProperty";
private const string UserFilePropertyDefaultValue = "Default";
[DisplayName("Custom Project User File Property")]
[DescriptionAttribute("This property is saved to the .user file.")]
[DefaultValue(UserFilePropertyDefaultValue)]
public string CustomUserFileProperty
{
get
{
string propertyValue = string.Empty;
// Try to get the current value from the .user file; if it does not yet exist, return a default value.
if (!sharePointProject.ProjectUserFileData.TryGetValue(UserFilePropertyId, out propertyValue))
{
propertyValue = UserFilePropertyDefaultValue;
}
return propertyValue;
}
set
{
// Do not save the default value.
if (value != UserFilePropertyDefaultValue)
{
sharePointProject.ProjectUserFileData[UserFilePropertyId] = value;
}
}
}
}
}
了解代码
为了确保每当 ProjectPropertiesRequested 事件发生时都使用 CustomProjectProperties 类的同一个实例,该代码示例在此事件第一次发生时,将属性对象添加到项目的 Annotations 属性中。 无论此事件什么时候再次发生,代码都将检索此对象。 有关使用 Annotations 属性将数据与项目关联的更多信息,请参见将自定义数据与 SharePoint 工具扩展相关联。
为了保留对属性值所做的更改,属性的 set 访问器将使用以下 API:
CustomUserFileProperty 使用 ProjectUserFileData 属性将其值保存到项目用户选项文件中。
CustomProjectFileProperty 使用 IVsBuildPropertyStorage.SetPropertyValue 方法将其值保存到项目文件中。
有关将数据保留在这些文件中的更多信息,请参见在 SharePoint 项目系统的扩展中保存数据。
指定自定义属性的行为
通过将 System.ComponentModel 命名空间中的特性应用于属性定义,可定义自定义属性在**“属性”**窗口中的显示方式和行为方式。 下列特性可用于多种方案:
DisplayNameAttribute:指定属性在**“属性”**窗口中的显示名称。
DescriptionAttribute:指定选择属性时显示在**“属性”**窗口底部的描述字符串。
DefaultValueAttribute:指定属性的默认值。
TypeConverterAttribute:指定**“属性”**窗口中显示的字符串与非字符串属性值之间的自定义转换。
EditorAttribute:指定用于修改属性的自定义编辑器。
编译代码
此示例需要对以下程序集的引用:
Microsoft.VisualStudio.SharePoint
Microsoft.VisualStudio.Shell
Microsoft.VisualStudio.Shell.Interop
Microsoft.VisualStudio.Shell.Interop.8.0
System.ComponentModel.Composition
部署扩展
若要部署扩展,请为要随此扩展分发的程序集和任何其他文件创建 Visual Studio 扩展 (VSIX) 包。 有关更多信息,请参见在 Visual Studio 中部署 SharePoint 工具扩展。