다음을 통해 공유


SharePoint 프로젝트 시스템의 확장에 데이터 저장

SharePoint 프로젝트 시스템을 확장하는 경우 SharePoint 프로젝트가 닫힌 후에도 유지되는 문자열 데이터를 저장할 수 있습니다. 이 데이터는 일반적으로 특정 프로젝트 항목 또는 프로젝트 자체와 연결됩니다.

유지할 필요가 없는 데이터가 있는 경우 IAnnotatedObject 인터페이스를 구현하는 SharePoint 도구 개체 모델의 어떠한 개체에도 데이터를 추가할 수 있습니다. 자세한 내용은 SharePoint 도구 확장과 사용자 지정 데이터 연결을 참조하십시오.

프로젝트 항목과 연결된 데이터 저장

프로젝트 항목에 추가하는 속성의 값과 같이 특정 SharePoint 프로젝트 항목과 연결된 데이터가 있는 경우 프로젝트 항목에 대한 .spdata 파일에 데이터를 저장할 수 있습니다. 이렇게 하려면 ISharePointProjectItem 개체의 ExtensionData 속성을 사용합니다. 이 속성에 추가한 데이터는 프로젝트 항목에 대한 .spdata 파일의 ExtensionData 요소에 저장됩니다. 자세한 내용은 ExtensionData 요소를 참조하십시오.

다음 코드 예제에서는 사용자 지정 SharePoint 프로젝트 항목 형식에 정의된 문자열 속성의 값을 ExtensionData 속성을 사용하여 저장하는 방법을 보여 줍니다. 보다 큰 예제의 컨텍스트에서 이 예제를 보려면 방법: 사용자 지정 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 메서드를 사용합니다. 다음 코드 예제에서는 이 메서드를 사용하여 프로젝트 속성의 값을 프로젝트 파일에 저장하는 방법을 보여 줍니다. 보다 큰 예제의 컨텍스트에서 이 예제를 보려면 방법: 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 자동화 개체 모델 또는 통합 개체 모델의 다른 형식으로 변환하는 방법에 대한 자세한 내용은 SharePoint 프로젝트 시스템 형식과 기타 Visual Studio 프로젝트 형식 간의 변환을 참조하십시오.

프로젝트 사용자 옵션 파일에 데이터 저장

프로젝트 사용자 옵션 파일에 데이터를 저장하려면 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 프로젝트 형식 간의 변환