연습: SharePoint 프로젝트 확장 만들기

이 연습에서는 SharePoint 프로젝트용 확장을 만드는 방법을 보여 줍니다. 프로젝트 확장을 사용하면 프로젝트 추가, 삭제 또는 이름 변경 등의 프로젝트 수준 이벤트에 응답할 수 있습니다. 또한 사용자 지정 속성을 추가하거나 속성 값이 변경될 때 응답할 수 있습니다. 프로젝트 항목 확장과 달리 프로젝트 확장은 특정 SharePoint 프로젝트 형식에 연결할 수 없습니다. 프로젝트 확장을 만들면 종류에 관계없이 Visual Studio에서 SharePoint 프로젝트를 열 때 확장이 로드됩니다.

이 연습에서는 Visual Studio에서 만든 모든 SharePoint 프로젝트에 추가되는 사용자 지정 부울 속성을 만듭니다. 이 속성이 True로 설정되면 새 속성이 이미지 리소스 폴더를 프로젝트에 추가하거나 매핑합니다. False로 설정된 경우에는 이미지 폴더가 있으면 이 폴더가 제거됩니다. 자세한 내용은 방법: 매핑된 폴더 추가 및 제거를 참조하십시오.

이 연습에서는 다음 작업을 수행합니다.

  • 다음을 수행하는 프로젝트용 Visual Studio 확장 만들기

    • 사용자 지정 프로젝트 속성을 속성 창에 추가합니다. 이 속성은 모든 SharePoint 프로젝트에 적용됩니다.

    • SharePoint 프로젝트 개체 모델을 사용하여 매핑된 폴더를 프로젝트에 추가합니다.

    • Visual Studio 자동화 개체 모델(DTE)을 사용하여 매핑된 폴더를 프로젝트에서 삭제합니다.

  • 프로젝트 속성의 확장 어셈블리를 배포하기 위한 VSIX(Visual Studio Extension) 패키지 빌드

  • 프로젝트 속성 디버깅 및 테스트

사전 요구 사항

이 연습을 완료하려면 개발 컴퓨터에 다음 구성 요소가 필요합니다.

프로젝트 만들기

이 연습을 완료하려면 두 프로젝트를 만들어야 합니다.

  • VSIX 패키지를 만들어 프로젝트 확장을 배포하기 위한 VSIX 프로젝트

  • 프로젝트 확장을 구현하는 클래스 라이브러리 프로젝트

먼저 프로젝트를 만들어 연습을 시작합니다.

VSIX 프로젝트를 만들려면

  1. Visual Studio를 시작합니다.

  2. 파일 메뉴에서 새로 만들기를 가리킨 다음 프로젝트를 클릭합니다.

  3. 새 프로젝트 대화 상자에서 Visual C# 또는 Visual Basic 노드를 확장한 다음 확장성 노드를 클릭합니다.

    참고

    확장성 노드는 Visual Studio 2010 SDK를 설치한 경우에만 사용할 수 있습니다. 자세한 내용은 이 항목의 앞부분에 나오는 사전 요구 사항 단원을 참조하십시오.

  4. 대화 상자 맨 위의 콤보 상자에서 .NET Framework 4를 선택합니다. SharePoint 도구 확장을 사용하려면 이 .NET Framework 버전의 기능이 필요합니다.

  5. VSIX 프로젝트 템플릿을 클릭합니다.

  6. 이름 상자에 ProjectExtensionPackage를 입력합니다.

  7. 확인을 클릭합니다.

    Visual Studio에서 솔루션 탐색기ProjectExtensionPackage 프로젝트를 추가합니다.

확장 프로젝트를 만들려면

  1. 솔루션 탐색기에서 솔루션 노드를 마우스 오른쪽 단추로 클릭한 다음 추가, 새 프로젝트를 차례로 클릭합니다.

    참고

    Visual Basic 프로젝트에서는 옵션 대화 상자, 프로젝트 및 솔루션, 일반에서 솔루션 항상 표시 확인란을 선택한 경우에만 솔루션 탐색기에 솔루션 노드가 표시됩니다.

  2. 새 프로젝트 대화 상자에서 Visual C# 또는 Visual Basic 노드를 확장하고 Windows를 클릭합니다.

  3. 대화 상자 맨 위의 콤보 상자에서 .NET Framework 4를 선택합니다.

  4. 클래스 라이브러리 프로젝트 템플릿을 선택합니다.

  5. 이름 상자에 ProjectExtension을 입력합니다.

  6. 확인을 클릭합니다.

    Visual Studio에서 솔루션에 ProjectExtension 프로젝트를 추가하고 기본 Class1 코드 파일을 엽니다.

  7. 프로젝트에서 Class1 코드 파일을 삭제합니다.

프로젝트 구성

프로젝트 확장을 만드는 코드를 작성하기 전에 코드 파일과 어셈블리 참조를 확장 프로젝트에 추가합니다.

프로젝트를 구성하려면

  1. ProjectExtension 프로젝트에 CustomProperty라는 새 코드 파일을 추가합니다.

  2. 프로젝트 메뉴에서 참조 추가를 클릭합니다.

  3. .NET 탭에서 Ctrl 키를 누르고 다음 어셈블리를 클릭한 다음 확인을 클릭합니다.

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

    • System.Windows.Forms

    • EnvDTE

  4. 솔루션 탐색기에서 ProjectExtension 프로젝트의 참조 폴더 아래에 있는 EnvDTE를 클릭합니다.

  5. 속성 창에서 Interop 형식 포함 속성을 False로 변경합니다.

새 SharePoint 프로젝트 속성 정의

프로젝트 확장 및 새 프로젝트 속성의 동작을 정의하는 클래스를 만듭니다. 새 프로젝트 확장을 정의하기 위해 클래스에서 ISharePointProjectExtension 인터페이스를 구현합니다. SharePoint 프로젝트용 확장을 정의하려는 경우 항상 이 인터페이스를 구현합니다. 또한 클래스에 ExportAttribute 메서드를 추가합니다. 이 특성을 사용하면 Visual Studio에서 ISharePointProjectExtension 구현을 찾아 로드할 수 있습니다. ISharePointProjectExtension 형식을 해당 특성의 생성자에 전달합니다.

새 SharePoint 프로젝트 속성을 정의하려면

  1. CustomProperty 코드 파일이 아직 열려 있지 않으면 이 파일을 편집하기 위해 두 번 클릭합니다.

  2. 파일에 다음 코드를 붙여 넣습니다.

    Imports System
    Imports System.Linq
    Imports System.ComponentModel
    Imports System.ComponentModel.Composition
    Imports System.Windows.Forms
    Imports Microsoft.VisualStudio.SharePoint
    Imports EnvDTE
    
    Namespace Contoso.SharePointProjectExtensions.MapImagesFolder
    
        ' Export attribute: Enables Visual Studio to discover and load this extension.
        ' MapImagesFolderProjectExtension class: Adds a new Map Images Folder property to any SharePoint project.
        <Export(GetType(ISharePointProjectExtension))> _
        Public Class MapImagesFolderProjectExtension
            Implements ISharePointProjectExtension
    
            Public Sub Initialize(ByVal projectService As ISharePointProjectService) Implements ISharePointProjectExtension.Initialize
                AddHandler projectService.ProjectPropertiesRequested, AddressOf Me.projectService_ProjectPropertiesRequested
            End Sub
    
            Private Sub projectService_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 = Nothing
            Private Const MapImagesFolderPropertyDefaultValue As Boolean = False
            Private Const MapImagesFolderPropertyId = "ContosoMapImagesFolderProperty"
    
            Public Sub New(ByVal myProject As ISharePointProject)
                sharePointProject = myProject
            End Sub
    
            ' Represents the new boolean property MapImagesFolder.
            ' True = Map an Images folder to the project if one does not already exist; otherwise, do nothing.
            ' False = Remove the Images folder from the project, if one exists; otherwise, do nothing.
            <DisplayName("Map Images Folder")> _
            <DescriptionAttribute("Specifies whether an Images folder is mapped to the SharePoint project.")> _
            <DefaultValue(MapImagesFolderPropertyDefaultValue)> _
            Public Property MapImagesFolder As Boolean
                Get
                    Dim propertyStringValue 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(MapImagesFolderPropertyId, propertyStringValue) Then
                        Return MapImagesFolderPropertyDefaultValue
                    Else
                        Return CBool(propertyStringValue)
                    End If
                End Get
    
                Set(ByVal value As Boolean)
                    If value Then
                        If Not ImagesMappedFolderInProjectExists(sharePointProject) Then
                            ' An Images folder is not mapped to the project, so map one.
                            Dim mappedFolder As IMappedFolder = sharePointProject.MappedFolders.Add(MappedFolderType.Images)
                            sharePointProject.ProjectService.Logger.WriteLine( _
                                mappedFolder.Name & " mapped folder added to the project.", LogCategory.Status)
                        End If
                    ElseIf (ImagesMappedFolderInProjectExists(sharePointProject) AndAlso UserSaysDeleteFile()) Then
                        ' An Images folder is mapped to the project and the user wants to remove it.
                        DeleteFolder()
                    End If
    
                    sharePointProject.ProjectUserFileData(MapImagesFolderPropertyId) = value.ToString()
                End Set
            End Property
    
            Private Function ImagesMappedFolderInProjectExists(ByVal sharePointProject As ISharePointProject) As Boolean
                Dim returnValue As Boolean = False
                For Each folder As IMappedFolder In sharePointProject.MappedFolders
                    ' Check to see if an Images folder is already mapped.
                    If (folder.FolderType = MappedFolderType.Images) Then
                        returnValue = True
                    End If
                Next
                Return returnValue
            End Function
    
            Private Function UserSaysDeleteFile() As Boolean
                ' Ask the user whether they want to delete the Images folder.
                Dim returnValue As Boolean = False
                If (MessageBox.Show("Do you want to delete the Images folder from the project?", _
                    "Delete the Images folder?", MessageBoxButtons.YesNo) = DialogResult.Yes) Then
                    returnValue = True
                End If
                Return returnValue
            End Function
    
            Private Sub DeleteFolder()
                ' The Visual Studio DTE object model is required to delete the mapped folder.
                Dim dteProject As EnvDTE.Project = _
                    sharePointProject.ProjectService.Convert(Of ISharePointProject, EnvDTE.Project)(sharePointProject)
                Dim targetFolderName As String = _
                    sharePointProject.MappedFolders.First(Function(mf) mf.FolderType = MappedFolderType.Images).Name
                Dim mappedFolderItem As EnvDTE.ProjectItem = dteProject.ProjectItems.Item(targetFolderName)
                mappedFolderItem.Delete()
    
                sharePointProject.ProjectService.Logger.WriteLine("Mapped Folder " & _
                    targetFolderName & " deleted", LogCategory.Status)
            End Sub
        End Class
    End Namespace
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.ComponentModel.Composition;
    using System.Windows.Forms;
    using Microsoft.VisualStudio.SharePoint;
    using EnvDTE;
    
    // Adds a new property called MapImagesFolder to any SharePoint project.
    // When MapImagesFolder is set to true, the Image folder is mapped to the project.
    // When MapImagesFolder is set to false, the Image folder is deleted from the project.
    namespace SP_Project_Extension
    {
        // Export attribute: Enables Visual Studio to discover and load this extension.
        [Export(typeof(ISharePointProjectExtension))]
    
        // Defines a new custom project property that applies to any SharePoint project.
        public class SPProjectExtension : ISharePointProjectExtension
        {
            // Implements ISharePointProjectService.Initialize, which determines the behavior of the new property.
            public void Initialize(ISharePointProjectService projectService)
            {
                // Handle events for when a project property is changed.
                projectService.ProjectPropertiesRequested +=
                    new EventHandler<SharePointProjectPropertiesRequestedEventArgs>(projectService_ProjectPropertiesRequested);
            }
    
            void projectService_ProjectPropertiesRequested(object sender, SharePointProjectPropertiesRequestedEventArgs e)
            {
                // Add a new property to the SharePoint project.
                e.PropertySources.Add((object)new ImagesMappedFolderProperty(e.Project));
            }
        }
    
        public class ImagesMappedFolderProperty
        {
            ISharePointProject sharePointProject = null;
            public ImagesMappedFolderProperty(ISharePointProject myProject)
            {
                sharePointProject = myProject;
            }
            static bool MapFolderSetting = false;
    
            [DisplayName("Map Images Folder")]
            [DescriptionAttribute("Specifies whether an Images folder is mapped to the SharePoint project.")]
            public bool MapImagesFolder
            // Represents the new boolean property MapImagesFolder.
            // True = Map an Images folder to the project if one does not already exist; otherwise, do nothing.
            // False = Remove the Images folder from the project, if one exists; otherwise, do nothing.
            {
                get
                {
                    // Get the current property value.
                    return MapFolderSetting;
                }
                set
                {
                    if (value)
                    {
                        if (!ImagesMappedFolderInProjectExists(sharePointProject))
                        {
                            // An Images folder is not mapped to the project, so map one.
                            IMappedFolder mappedFolder1 = sharePointProject.MappedFolders.Add(MappedFolderType.Images);
                            // Add a note to the logger that a mapped folder was added.
                            sharePointProject.ProjectService.Logger.WriteLine("Mapped Folder added:" + mappedFolder1.Name, LogCategory.Status);
                        }
                    }
                    else
                    {
                        if (ImagesMappedFolderInProjectExists(sharePointProject) && UserSaysDeleteFile())
                        {
                            // An Images folder is mapped to the project and the user wants to remove it.
                            // The Visual Studio DTE object model is required to delete the mapped folder.
                            // Reference the Visual Studio DTE model, get handles for the SharePoint project and project items.
                            EnvDTE.Project dteProject = sharePointProject.ProjectService.Convert<ISharePointProject, EnvDTE.Project>(sharePointProject);
                            string targetFolderName = sharePointProject.MappedFolders.First(mf => mf.FolderType == MappedFolderType.Images).Name;
                            EnvDTE.ProjectItem mappedFolderItem = dteProject.ProjectItems.Item(targetFolderName);
                            mappedFolderItem.Delete();
                            sharePointProject.ProjectService.Logger.WriteLine("Mapped Folder " + targetFolderName + " deleted", LogCategory.Status);
                        }
                    }
                    MapFolderSetting = value;
                }
    
            }
    
            private bool ImagesMappedFolderInProjectExists(ISharePointProject sharePointProject)
            {
                bool retVal = false;
                foreach (IMappedFolder folder in sharePointProject.MappedFolders)
                {
                    // Check to see if an Images folder is already mapped.
                    if (folder.FolderType == MappedFolderType.Images)
                        retVal = true;
                }
                return retVal;
            }
    
            private bool UserSaysDeleteFile()
            {
                // Prompt the user whether they want to delete the Images folder.
                bool retVal = false;
                if (MessageBox.Show("Do you want to delete the Images folder from the project?", "Delete the Images folder?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    retVal = true;
                }
                return retVal;
    
            }
        }
    }
    

솔루션 빌드

다음 단계로 솔루션을 빌드하여 솔루션이 오류 없이 컴파일되는지 확인합니다.

솔루션을 빌드하려면

  • 빌드 메뉴에서 솔루션 빌드를 클릭합니다.

프로젝트 속성 확장을 배포하기 위한 VSIX 패키지 만들기

프로젝트 확장을 배포하려면 솔루션에서 VSIX 프로젝트를 사용하여 VSIX 패키지를 만듭니다. 먼저 VSIX 프로젝트에 포함된 source.extension.vsixmanifest 파일을 수정하여 VSIX 패키지를 구성합니다. 그런 다음 솔루션을 빌드하여 VSIX 패키지를 만듭니다.

VSIX 패키지를 구성하고 만들려면

  1. 솔루션 탐색기에서 source.extension.vsixmanifest 파일을 두 번 클릭합니다.

    Visual Studio가 매니페스트 편집기에서 파일을 엽니다. 이 편집기는 매니페스트에서 XML을 편집하는 데 사용할 수 있는 UI를 제공합니다. 이 정보는 나중에 확장 관리자에 표시됩니다. extension.vsixmanifest 파일은 모든 VSIX 패키지에 필요합니다. 이 파일에 대한 자세한 내용은 VSIX Extension Schema Reference를 참조하십시오.

  2. 제품 이름 상자에 Custom Project Property를 입력합니다.

  3. 만든 이 상자에 Contoso를 입력합니다.

  4. 설명 상자에 A custom SharePoint project property that toggles the mapping of the Images resource folder to the project.를 입력합니다.

  5. 편집기의 콘텐츠 섹션에서 콘텐츠 추가 단추를 클릭합니다.

  6. 콘텐츠 형식 선택 드롭다운 상자에서 MEF 구성 요소를 선택합니다.

    참고

    이 값은 extension.vsixmanifest 파일의 MEFComponent 요소에 해당합니다. 이 요소는 VSIX 패키지의 확장 어셈블리 이름을 지정합니다. 자세한 내용은 MEFComponent Element (VSX Schema)를 참조하십시오.

  7. 소스 선택 섹션에서 프로젝트 옵션을 클릭한 다음 드롭다운 상자에서 ProjextExtension을 선택합니다.

    이 값은 프로젝트에서 빌드하는 어셈블리의 이름을 식별합니다.

  8. 작업이 끝나면 확인을 클릭하여 콘텐츠 추가 대화 상자를 닫습니다.

  9. 작업이 끝나면 파일 메뉴의 모두 저장을 클릭한 다음 매니페스트 디자이너를 닫습니다.

  10. 빌드 메뉴에서 솔루션 빌드를 클릭합니다. 프로젝트가 오류 없이 컴파일되는지 확인합니다.

  11. 솔루션 탐색기에서 ProjectExtensionPackage 프로젝트를 클릭하고, 모든 파일 표시 단추를 클릭한 다음 ProjectExtensionPackage 프로젝트의 빌드 출력 폴더를 엽니다. 이 폴더에는 ProjectExtensionPackage.vsix라는 이름의 파일이 포함되어 있어야 합니다.

    기본적으로 빌드 출력 경로는 프로젝트 파일이 포함된 폴더 아래에 있는 .. \bin\Debug 폴더입니다.

프로젝트 속성 테스트

이제 사용자 지정 프로젝트 속성을 테스트할 준비가 되었습니다. 가장 쉬운 방법은 실험 모드의 Visual Studio 인스턴스에서 새 프로젝트 속성 확장을 디버깅하고 테스트하는 것입니다. 이 인스턴스는 VSIX 또는 기타 확장성 프로젝트를 실행할 때 만들어지는 Visual Studio의 인스턴스입니다. 프로젝트를 디버깅한 후 시스템에 확장을 설치한 다음 Visual Studio의 일반 인스턴스에서 확장을 계속 디버깅하고 테스트할 수 있습니다.

실험 모드의 Visual Studio 인스턴스에서 확장을 디버깅 및 테스트하려면

  1. 관리자 자격 증명으로 Visual Studio를 다시 시작하고 ProjectExtensionPackage 솔루션을 엽니다.

  2. F5 키를 눌러 프로젝트의 디버그 빌드를 시작합니다.

    Visual Studio에서는 확장을 %UserProfile%\AppData\Local\Microsoft\VisualStudio\10.0Exp\Extensions\Contoso\Custom Project Property\1.0에 설치하고 실험 모드의 Visual Studio 인스턴스를 시작합니다.

  3. 실험 모드의 Visual Studio 인스턴스에서 팜 솔루션 SharePoint 프로젝트(예: 모듈)를 새로 만듭니다. 다른 값에 대해서는 마법사에서 제공하는 기본값을 사용합니다.

  4. 솔루션 탐색기에서 프로젝트 노드를 클릭합니다.

    새 사용자 지정 속성 Map Images Folder속성 창에 나타나고, 이 속성의 기본값은 False입니다.

  5. Map Images Folder를 True로 변경합니다.

    이미지 리소스 폴더가 SharePoint 프로젝트에 추가됩니다.

  6. Map Images Folder를 False로 변경합니다.

    이미지 리소스 폴더가 SharePoint 프로젝트에서 삭제됩니다.

  7. 실험 모드의 Visual Studio 인스턴스를 닫습니다.

참고 항목

기타 리소스

SharePoint 프로젝트 확장

방법: SharePoint 프로젝트에 속성 추가

SharePoint 프로젝트 시스템 형식과 기타 Visual Studio 프로젝트 형식 간의 변환

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

SharePoint 도구 확장과 사용자 지정 데이터 연결