儲存 SharePoint 專案系統擴充功能的資料
擴充 SharePoint 專案系統時,您可以儲存 SharePoint 專案關閉後仍然存在的字串資料。 這種資料通常與特定專案項目或專案本身相關聯。
如果有不需要保存的資料,您可以將資料加入至實作 IAnnotatedObject 介面的 SharePoint 工具物件模型中的任何物件。 如需詳細資訊,請參閱讓自訂資料與 SharePoint 工具擴充功能產生關聯。
儲存與專案項目關聯的資料
如果有與特定 SharePoint 專案項目關聯的資料,例如專案項目中加入的屬性值,您可以將資料儲存到專案項目的 .spdata 檔案。 若要這麼做,請使用 ISharePointProjectItem 物件的 ExtensionData 屬性。 您加入至此屬性的資料會儲存在專案項目 (Item) 之 .spdata 檔案的 ExtensionData 項目 (Element) 中。 如需詳細資訊,請參閱 ExtensionData 項目。
下列程式碼範例會示範如何使用 ExtensionData 屬性來儲存在自訂 SharePoint 專案項目類型中定義的字串屬性值。 若要在完整的範例內容中查看這個範例,請參閱 HOW TO:將屬性加入至自訂 SharePoint 專案項目類型。
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
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);
}
}
}
儲存與專案關聯的資料
如果有專案層級資料,例如 SharePoint 專案中加入的屬性值,您可以將資料儲存到專案檔 (.csproj 檔案或 .vbproj 檔案) 或專案使用者選項檔 (.csproj.user 檔案或 .vbproj.user 檔案)。 您選擇儲存資料的檔案取決於您要使用資料的方式:
如果想要讓所有開啟 SharePoint 專案的開發人員都能使用資料,請將資料儲存到專案檔。 這個檔案永遠會簽入原始檔控制資料庫,因此其他簽出專案的開發人員都能使用這個檔案中的資料。
如果只想要讓目前在 Visual Studio 中開啟 SharePoint 專案的開發人員使用資料,請將資料儲存到專案使用者選項檔。 這個檔案通常不會簽入原始檔控制資料庫,因此其他簽出專案的開發人員無法使用這個檔案中的資料。
將資料儲存到專案檔
若要將資料儲存到專案檔,請將 ISharePointProject 物件轉換成 IVsBuildPropertyStorage 物件,然後使用 IVsBuildPropertyStorage.SetPropertyValue 方法。 下列程式碼範例會示範如何使用這個方法來將專案屬性值儲存到專案檔。 若要在完整的範例內容中查看這個範例,請參閱 HOW TO:將屬性加入至 SharePoint 專案。
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 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);
}
}
}
如需將 ISharePointProject 物件轉換成 Visual Studio Automation 物件模型或整合物件模型中的其他類型的詳細資訊,請參閱在 SharePoint 專案系統類型與其他 Visual Studio 專案類型之間轉換。
將資料儲存到專案使用者選項檔
若要將資料儲存到專案使用者選項檔,請使用 ISharePointProject 物件的 ProjectUserFileData 屬性。 下列程式碼範例會示範如何使用這個屬性來將專案屬性值儲存到專案使用者選項檔。 若要在完整的範例內容中查看這個範例,請參閱 HOW TO:將屬性加入至 SharePoint 專案。
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
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;
}
}
}