SharePoint プロジェクト システムの拡張機能におけるデータの保存

SharePoint プロジェクト システムを拡張した場合、SharePoint プロジェクトを閉じた後も保持される文字列データを保存できます。通常、データは、特定のプロジェクト項目またはプロジェクト自体に関連付けられています。

保持する必要のないデータがある場合は、IAnnotatedObject インターフェイスを実装する SharePoint ツールのオブジェクト モデル内の任意のオブジェクトにそのデータを追加できます。詳細については、「カスタム データの SharePoint ツールの拡張機能への関連付け」を参照してください。

プロジェクト項目に関連付けられているデータの保存

プロジェクト項目に追加したプロパティの値など、特定の SharePoint プロジェクト項目に関連付けられているデータがある場合は、そのデータをプロジェクト項目の .spdata ファイルに保存できます。これを行うには、ISharePointProjectItem オブジェクトの ExtensionData プロパティを使用します。このプロパティに追加するデータは、プロジェクト項目の .spdata ファイルの ExtensionData 要素に保存されます。詳細については、「ExtensionData 要素」を参照してください。

ExtensionData プロパティを使用してカスタムの SharePoint プロジェクト項目の種類で定義されている文字列プロパティの値を保存する方法を次のコード例に示します。この例のコンテキストを確認するには、「方法: プロパティをカスタムの 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 プロジェクトを開いている現在の開発者だけがデータを使用できるようにするには、データをプロジェクト ユーザー オプション ファイルに保存します。通常、このファイルはソース管理データベースにチェックインされないため、このファイルに含まれるデータは、プロジェクトをチェックアウトする他の開発者には使用できません。

Ff406949.collapse_all(ja-jp,VS.110).gifプロジェクト ファイルへのデータの保存

プロジェクト ファイルにデータを保存するには、ISharePointProject オブジェクトを IVsBuildPropertyStorage オブジェクトに変換して、IVsBuildPropertyStorage.SetPropertyValue メソッドを使用します。このメソッドを使用して、プロジェクト プロパティの値をプロジェクト ファイルに保存する方法を次のコード例に示します。この例のコンテキストを確認するには、「方法: 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 のオートメーション オブジェクト モデルまたは Visual Studio の統合オブジェクト モデルの他の種類のオブジェクトに変換する方法の詳細については、「SharePoint プロジェクト システムと他の Visual Studio プロジェクトの間の型変換」を参照してください。

Ff406949.collapse_all(ja-jp,VS.110).gifプロジェクト ユーザー オプション ファイルへのデータの保存

プロジェクト ユーザー オプション ファイルにデータを保存するには、ISharePointProject オブジェクトの ProjectUserFileData プロパティを使用します。このプロパティを使用して、プロジェクト プロパティの値をプロジェクト ユーザー オプション ファイルに保存する方法を次のコード例に示します。この例のコンテキストを確認するには、「方法: 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;
        }
    }
}                

参照

概念

SharePoint プロジェクト システムの拡張

カスタム データの SharePoint ツールの拡張機能への関連付け

SharePoint プロジェクト システムと他の Visual Studio プロジェクトの間の型変換