다음을 통해 공유


방법: 사용자 지정 SharePoint 프로젝트 항목 형식에 속성 추가

사용자 지정 SharePoint 프로젝트 항목 형식을 정의하는 경우 프로젝트 항목에 속성을 추가할 수 있습니다. 솔루션 탐색기에서 프로젝트 항목을 선택하면 속성 창에 해당 속성이 표시됩니다.

다음 단계에서는 사용자 고유의 SharePoint 프로젝트 항목 형식이 이미 정의되어 있는 것으로 가정합니다. 자세한 내용은 방법: SharePoint 프로젝트 항목 형식 정의를 참조하십시오.

프로젝트 항목 형식의 정의에 속성을 추가하려면

  1. 사용자 지정 프로젝트 항목 형식에 추가할 속성을 나타내는 공용 속성이 있는 클래스를 정의합니다. 사용자 지정 프로젝트 항목 형식에 여러 속성을 추가하려는 경우 모든 속성을 동일한 클래스 또는 서로 다른 여러 클래스에 정의할 수 있습니다.

  2. ISharePointProjectItemTypeProvider 구현의 InitializeType 메서드에서 projectItemTypeDefinition 매개 변수의 ProjectItemPropertiesRequested 이벤트를 처리합니다.

  3. ProjectItemPropertiesRequested 이벤트의 이벤트 처리기에서 사용자 지정 속성 클래스의 인스턴스를 이벤트 인수 매개 변수의 PropertySources 컬렉션에 추가합니다.

예제

다음 코드 예제에서는 Example Property 속성을 추가하는 방법을 보여 줍니다.

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint

Namespace Contoso.Examples.ProjectItemTypeWithProperty

    <Export(GetType(ISharePointProjectItemTypeProvider))> _
    <SharePointProjectItemType("Contoso.ExampleProjectItemType")> _
    <SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")> _
    Friend Class ExampleProjectItemTypeWithProperty
        Implements ISharePointProjectItemTypeProvider

        Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
            Implements ISharePointProjectItemTypeProvider.InitializeType
            projectItemTypeDefinition.Name = "ExampleProjectItemType"
            projectItemTypeDefinition.SupportedDeploymentScopes = _
                SupportedDeploymentScopes.Site Or SupportedDeploymentScopes.Web
            projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All

            AddHandler projectItemTypeDefinition.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.ProjectItemTypeWithProperty
{
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("Contoso.ExampleProjectItemType")]
    [SharePointProjectItemIcon("ExampleProjectItemType.ProjectItemIcon.ico")]
    internal class ExampleProjectItemTypeWithProperty : ISharePointProjectItemTypeProvider
    {
        public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
        {
            projectItemTypeDefinition.Name = "ExampleProjectItemType";
            projectItemTypeDefinition.SupportedDeploymentScopes =
                SupportedDeploymentScopes.Site | SupportedDeploymentScopes.Web;
            projectItemTypeDefinition.SupportedTrustLevels = SupportedTrustLevels.All;

            projectItemTypeDefinition.ProjectItemPropertiesRequested += 
                projectItemTypeDefinition_ProjectItemPropertiesRequested;
        }

        void projectItemTypeDefinition_ProjectItemPropertiesRequested(object sender, 
            SharePointProjectItemPropertiesRequestedEventArgs e)
        {
            CustomProperties property;

            // If the properties object already exists, get it from the project item's annotations.
            if (!e.ProjectItem.Annotations.TryGetValue(out property))
            {
                // Otherwise, create a new properties object and add it to the annotations.
                property = new CustomProperties(e.ProjectItem);
                e.ProjectItem.Annotations.Add(property);
            }

            e.PropertySources.Add(property);
        }
    }

    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 프로젝트 시스템의 확장에 데이터 저장을 참조하십시오.

사용자 지정 속성의 동작 지정

System.ComponentModel 네임스페이스의 특성을 속성 정의에 적용하여 사용자 지정 속성이 속성 창에서 표시되고 동작하는 방식을 정의할 수 있습니다. 다음 특성은 여러 시나리오에서 유용합니다.

  • DisplayNameAttribute. 속성 창에 표시되는 속성의 이름을 지정합니다.

  • DescriptionAttribute. 속성을 선택할 때 속성 창의 아래쪽에 나타나는 설명 문자열을 지정합니다.

  • DefaultValueAttribute. 속성의 기본값을 지정합니다.

  • TypeConverterAttribute. 속성 창에 나타나는 문자열과 문자열 이외의 속성 값 사이의 사용자 지정 변환을 지정합니다.

  • EditorAttribute. 속성을 수정하는 데 사용할 사용자 지정 편집기를 지정합니다.

코드 컴파일

이 코드 예제에는 다음 어셈블리에 대한 참조가 있는 클래스 라이브러리 프로젝트가 필요합니다.

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

프로젝트 항목 배포

다른 개발자가 프로젝트 항목을 사용할 수 있도록 하려면 프로젝트 템플릿이나 프로젝트 항목 템플릿을 만듭니다. 자세한 내용은 SharePoint 프로젝트 항목에 대한 항목 템플릿 및 프로젝트 템플릿 만들기를 참조하십시오.

프로젝트 항목을 배포하려면 어셈블리, 템플릿 및 프로젝트 항목과 함께 배포할 다른 모든 파일에 대한 VSIX(Visual Studio Extension) 패키지를 만듭니다. 자세한 내용은 Visual Studio에서 SharePoint 도구에 대한 확장 배포를 참조하십시오.

참고 항목

작업

방법: SharePoint 프로젝트 항목 형식 정의

기타 리소스

방법: 사용자 지정 SharePoint 프로젝트 항목 형식에 바로 가기 메뉴 항목 추가

사용자 지정 SharePoint 프로젝트 항목 형식 정의