Aracılığıyla paylaş


İzlenecek yol: SharePoint proje uzantısı oluşturma

Bu izlenecek yolda SharePoint projeler için bir uzantı nasıl gösterir.Proje ne zaman eklenen, silinen veya yeniden adlandırılmış gibi proje düzeyi olaylara yanıt vermek için bir proje uzantısı kullanın.Özel özellikler ekleyebilir veya özellik değeri değiştiğinde yanıt.Proje öğesi uzantılarının aksine, belirli bir SharePoint proje türüyle ilgili proje uzantıları olamaz.Proje uzantısı oluşturduğunuzda, uzantınızı yüklediği her türlü SharePoint proje açıldığında Visual Studio.

Bu yönergede oluşturulmuş herhangi bir SharePoint projeye eklenen özel bir Boolean özelliği oluşturacak Visual Studio.Ayarlandığında True, yeni bir özellik ekler veya projenizin görüntüleri kaynak klasörüne eşler.Ayarlandığında False, varsa, Resimler klasörü kaldırıldı.Daha fazla bilgi için bkz. Nasıl yapılır: ekleme ve eşlenen klasörleri kaldırma.

Bu izlenecek yolda aşağıdaki görevler gösterilir:

  • Oluşturma bir Visual Studio uzantısı SharePoint projeler için şunları yapar:

    • Özel proje özellik Properties penceresine ekler.Herhangi bir SharePoint proje özelliği uygulanır.

    • Eşlenmiş bir klasörde bir projeye eklemek için SharePoint project nesne modelini kullanır.

    • Kullanan Visual Studio Otomasyon nesne modeli (dte) projeden eşlenmiş klasörü silin.

  • Oluşturma bir Visual Studio proje özelliğin uzantısı derleme dağıtmak için paket uzantısı (VSIX).

  • Hata ayıklama ve sınama proje özelliği.

Önkoşullar

Geliştirme bilgisayarında Bu izlenecek yolu tamamlamak için aşağıdaki bileşenler gerekmektedir:

Proje Oluşturma

Bu izlenecek yolu tamamlamak için iki proje oluşturmanız gerekir:

  • Proje uzantısı dağıtmak için VSIX paketi oluşturmak için bir VSIX proje.

  • Proje uzantısı uygulayan bir sınıf kitaplığı projesi.

İzlenecek yol, projeler oluşturmaya başlayın.

VSIX projesi oluşturmak için

  1. Visual Studio uygulamasını başlatın.

  2. Menü çubuğunda Seç Dosya, Yeni, Proje.

  3. İçinde Yeni Proje iletişim kutusunda, genişletme Visual C# veya Visual Basic düğümler ve sonra seçin genişletilebilirlik düğümü.

    [!NOT]

    Bu düğüm, Visual Studio sdk yüklüyorsanız kullanılabilir.Daha fazla bilgi için bu konudaki Önkoşullar bölümüne bakın.

  4. İletişim kutusunun üstündeki seçin .net Framework 4.5 .net Framework sürümü listesinde ve seçin VSIX projesi şablonu.

  5. İçinde adı kutusuna girin ProjectExtensionPackageve sonra Tamam düğmesi.

    ProjectExtensionPackage proje görünür Çözüm Gezgini.

Uzantı projesi oluşturmak için

  1. İçinde Çözüm Gezgini, çözüm düğümüne kısayol menüsünü açın, seçim Ekleve sonra seçin Yeni bir proje.

    [!NOT]

    İçinde Visual Basic projeleri, çözüm düğümüne görünür Çözüm Gezgini sadece her zaman çözümü Göster onay kutusu seçildiğinde, General, Projects and Solutions, Options Dialog Box.

  2. İçinde Yeni Proje iletişim kutusunda, genişletme Visual C# veya Visual Basic düğümleri ve seçin Windows.

  3. İletişim kutusunun üstündeki seçim .net Framework 4.5 .net Framework sürümü listesinde ve seçin Sınıf kitaplığı proje şablonu.

  4. İçinde adı kutusuna girin ProjectExtensionve sonra Tamam düğmesi.

    Visual Studioekler ProjectExtension projesi çözüme ve varsayılan Class1 kod dosyasını açar.

  5. Projeden Class1 kod dosyasını silin.

Projeyi Yapılandırma

Proje uzantısı oluşturmak için kod yazmadan önce kod dosyaları ve uzantı projesi derleme başvurularını ekleyin.

Projeyi Yapılandırma

  1. Adlı bir kod dosyasına ekleyin CustomProperty ProjectExtension projesi.

  2. Kısayol menüsünü açma ProjectExtension proje ve sonra Add Reference.

  3. İçinde Reference Manager-CustomProperty iletişim kutusunda, seçim Framework düğümü ve System.ComponentModel.Composition ve System.Windows.Forms derlemelerine yanındaki onay kutusunu seçin.

  4. Seçim uzantıları düğümü, Microsoft.VisualStudio.SharePoint ve EnvDTE derlemeler yanındaki onay kutusunu seçin ve sonra seçin Tamam düğmesi.

  5. İçinde Çözüm Gezginialtında başvuruları için klasör ProjectExtension projesi, seçim EnvDTE.

  6. İçinde özelliklerini Değiştir penceresinde Birlikte çalışma türlerini katıştır özelliğini yanlış.

Yeni SharePoint proje özelliği tanımlama

Proje uzantısı ve yeni proje özellik davranışını tanımlayan bir sınıf oluşturun.Yeni Proje uzantısı olan sınıfın uyguladığı tanımlamak için ISharePointProjectExtension arabirim.SharePoint proje için bir uzantısı tanımlamak istediğinizde bu arabirimi uygulayın.Ayrıca, eklemek ExportAttribute sınıfına.Bu öznitelik sağlar Visual Studio bulmak ve yüklemek için ISharePointProjectExtension uygulaması.Pass ISharePointProjectExtension türü öznitelik oluşturucusu.

Yeni SharePoint proje özelliği tanımlamak için

  • CustomProperty kod dosyasına aşağıdaki kodu yapıştırın.

    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;
    
            }
        }
    }
    

Çözüm oluşturma

Ardından, hatasız derlendiğinden emin olmak için bir çözüm oluşturun.

Çözümü oluşturmak için

  • Menü çubuğunda Seç Yapı, Build Solution.

Proje özellik uzantısı dağıtmak için VSIX paketi oluşturma

Proje uzantısı dağıtmak için VSIX paketi oluşturmak için çözümünüzdeki VSIX projesi kullanın.İlk olarak, VSIX projesinde bulunan source.extension.vsixmanifest dosyasını değiştirerek VSIX paketi yapılandırın.Ardından, çözüm oluşturma tarafından VSIX paketi oluşturun.

VSIX paketi oluşturmak ve yapılandırmak için

  1. İçinde Çözüm Gezginisource.extension.vsixmanifest dosya için kısayol menüsünü açın ve sonra seçin açık düğmesi.

    Visual Studiodosya bildirim Tasarımcısı'nda açılır.Görüntülenen bilgileri meta veri sekmesi de görüntülenir uzantıları ve güncelleştirmeleri. Tüm VSIX paketi extension.vsixmanifest dosyası gerektirir.Bu dosya hakkında daha fazla bilgi için bkz: VSIX uzantı Şeması Başvurusu.

  2. İçinde Product Name kutusuna girin Özel proje özelliği.

  3. İçinde Yazar kutusuna girin Contoso.

  4. İçinde Açıklama kutusuna girin özel SharePoint proje özelliği, proje için görüntüleri kaynak klasör eşlemesini değiştirir.

  5. Seçim varlıklarını sekmesini tıklatın ve sonra seçin Yeni düğmesi.

    Yeni kıymet eklemek iletişim kutusu görüntülenir.

  6. İçinde türü listesinde, seçim Microsoft.VisualStudio.MefComponent.

    [!NOT]

    Bu değeri karşılık gelen MEFComponent extension.vsixmanifest dosyasındaki öğesi.Bu öğe, VSIX paketinde uzantısı derlemenin adını belirtir.Daha fazla bilgi için bkz. MEFComponent Element.

  7. İçinde kaynak listesinde, seçim Geçerli çözümde bir proje seçenek düğmesi.

  8. İçinde Proje listesinde, seçim ProjectExtension.

    Bu değer, projede oluşturmakta olduğunuz derlemenin adını tanımlar.

  9. Seçim Tamam kapatmak için Ekleme yeni kıymet iletişim kutusu.

  10. Menü çubuğunda Seç Dosya, Tümünü Kaydet bitirmek ve açtığınızda bildirim designer'ı kapatın.

  11. Menü çubuğunda Seç Yapı, Build Solutionve sonra projeyi hatasız derlendiğinden emin olun.

  12. İçinde Çözüm Gezgini, kısayol menüsünü açmak ProjectExtensionPackage proje ve seçin Dosya Gezgini'nde klasör açmak düğmesi.

  13. İçinde Dosya Explorer, ProjectExtensionPackage projenin yapı çıktı klasörüne açın ve sonra klasör, ProjectExtensionPackage.vsix adlı bir dosya içerir doğrulayın.

    Varsayılan olarak, yapı çıktı klasörüne değil...Proje dosyanızı içeren klasörü altındaki \bin\Debug klasörünü.

Proje özellik sınama

Şimdi özel proje özelliği test etmek hazırsınız demektir.Hata ayıklama ve yeni proje özellik uzantısı içinde deneysel örneği test etmek en kolay Visual Studio.Bu örneği Visual Studio bir VSIX veya diğer genişletilebilirlik proje çalıştırdığınızda oluşturulur.Projenin hatalarýný sonra sisteminizde uzantısı yüklemek ve normal bir örneği sınamak ve hata ayıklama devam Visual Studio.

Hata ayıklama ve uzantısı Visual Studio'nun deneysel örneği sınamak için

  1. Yeniden Visual Studio ile yönetimsel kimlik bilgileri ve sonra ProjectExtensionPackage çözümü açın.

  2. Projenin hata ayıklama yapı seçerek Başlat F5 anahtar veya menü çubuğu, seçme Debug, Start Debugging komutunu.

    Visual Studio%UserProfile%\AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\Contoso\Custom proje Property\1.0 uzantısını yükler ve deneysel örneği başlar Visual Studio.

  3. Deneysel örneği de Visual StudioSharePoint proje grubu çözüm oluşturmak ve Sihirbazı'ndaki diğer değerler için varsayılan değerleri kullanın.

    1. Menü çubuğunda Seç Dosya, Yeni, Proje.

    2. Üstündeki Yeni Proje iletişim kutusunda, seçim .net Framework 3.5 .net Framework sürümlerinin listesi.

      SharePoint araç uzantılarını gerektiren bu sürümündeki özellikleri .NET Framework.

    3. Altında şablonları düğümü genişletin Visual C# veya Visual Basic düğümü, seçin SharePoint düğümü ve sonra seçin 2010 düğümü.

    4. Seçin SharePoint 2010 proje , şablon ve enter ModuleTest projenizin adı.

  4. İçinde Çözüm Gezgini, seçim ModuleTest projesi düğümünü.

    Yeni bir özel özellik Harita Resimler klasörünü görünür özelliklerini penceresinde varsayılan değeri olan False.

  5. Bu özelliğin değerini değiştirmek True.

    Görüntüleri kaynak klasör SharePoint projeye eklenir.

  6. Bu özelliğin değerini geri değiştirme False.

    İsterseniz Evet düğmesini Resimler klasörünü silmek? iletişim kutusunda, kaynak klasörü SharePoint projeden silinmiş görüntüler.

  7. Deneysel örneği kapatmak Visual Studio.

Ayrıca bkz.

Kavramlar

SharePoint projeleri genişletme

Nasıl yapılır: SharePoint projeleri için özellik eklemek

SharePoint proje sistem türleri ve diğer Visual Studio proje türleri arasında dönüştürme

SharePoint proje sistem uzantılarında verilerini kaydetme

Özel verileri SharePoint Araçlar uzantıları ile ilişkilendirme