Partager via


Comment : créer une extension de projet SharePoint

Créez une extension de projet si vous avez l'intention d'ajouter une fonctionnalité à un projet SharePoint ouvert dans Visual Studio. Pour plus d'informations, consultez Extension du système de projet SharePoint.

Pour créer une extension de projet

  1. Créez un projet Bibliothèque de classes.

  2. Ajoutez des références aux assemblys suivants :

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. Définissez une classe qui implémente l'interface ISharePointProjectExtension.

  4. Ajoutez l'attribut System.ComponentModel.Composition.ExportAttribute à la classe. Cet attribut permet à Visual Studio de découvrir et de charger votre implémentation ISharePointProjectExtension. Passez le type ISharePointProjectExtension au constructeur d'attribut.

  5. Dans votre implémentation de la méthode Initialize, utilisez les membres du paramètre projectService pour définir le comportement de votre extension. Ce paramètre est un objet ISharePointProjectService qui fournit l'accès aux événements définis dans l'interface ISharePointProjectEvents.

Exemple

L'exemple de code suivant montre comment créer une extension de projet simple qui gère la plupart des événements de projet SharePoint définis par l'interface ISharePointProjectEvents. Pour tester le code, créez un projet SharePoint dans Visual Studio, puis ajoutez d'autres projets à la solution, modifiez les valeurs de propriété du projet, ou supprimez ou excluez un projet. L'extension vous informe des événements en écrivant des messages dans les fenêtres Sortie et Liste d'erreurs.

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

Namespace Contoso.ExampleProjectExtension
    <Export(GetType(ISharePointProjectExtension))> _
    Class ExampleProjectExtension
        Implements ISharePointProjectExtension

        Private WithEvents projectService As ISharePointProjectService

        Public Sub Initialize(ByVal projectService As ISharePointProjectService) _
            Implements ISharePointProjectExtension.Initialize
            Me.projectService = projectService
        End Sub

        ' A project was added.
        Private Sub projectService_ProjectAdded(ByVal sender As Object, ByVal e As SharePointProjectEventArgs) _
            Handles projectService.ProjectAdded
            Dim project As ISharePointProject = CType(sender, ISharePointProject)
            Dim message As String = String.Format("The following project was added: {0}", e.Project.Name)
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
        End Sub

        ' A project was loaded in the IDE.
        Private Sub projectService_ProjectInitialized(ByVal sender As Object, ByVal e As SharePointProjectEventArgs) _
            Handles projectService.ProjectInitialized
            Dim project As ISharePointProject = CType(sender, ISharePointProject)
            Dim message As String = String.Format("The following project is being initialized: {0}", e.Project.Name)
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
        End Sub

        ' The name of a property was changed.
        Private Sub projectService_ProjectNameChanged(ByVal sender As Object, ByVal e As NameChangedEventArgs) _
            Handles projectService.ProjectNameChanged
            Dim project As ISharePointProject = CType(sender, ISharePointProject)
            Dim message As String = String.Format("The project named {0} was changed to {1}.", e.OldName, project.Name)
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
        End Sub

        ' A project property value was changed.
        Private Sub ProjectPropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) _
            Handles projectService.ProjectPropertyChanged
            Dim project As ISharePointProject = CType(sender, ISharePointProject)
            Dim message As String = String.Format("The following property of the {0} project was changed: {1}",
                project.Name, e.PropertyName)
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
        End Sub

        ' A project is being removed or unloaded.
        Private Sub projectService_ProjectRemoved(ByVal sender As Object, ByVal e As SharePointProjectEventArgs) _
            Handles projectService.ProjectRemoved
            Dim project As ISharePointProject = CType(sender, ISharePointProject)
            Dim message As String = String.Format("The following project is being removed or unloaded: {0}", e.Project.Name)
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
        End Sub

        ' A project was removed or unloaded.
        Private Sub projectService_ProjectDisposing(ByVal sender As Object, ByVal e As SharePointProjectEventArgs) _
            Handles projectService.ProjectDisposing
            Dim project As ISharePointProject = CType(sender, ISharePointProject)
            Dim message As String = String.Format("The following project was removed or unloaded: {0}", e.Project.Name)
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message)
        End Sub
    End Class
End Namespace
using Microsoft.VisualStudio.SharePoint;
using System;
using System.ComponentModel;
using System.ComponentModel.Composition;

namespace Contoso.ExampleProjectExtension
{
    [Export(typeof(ISharePointProjectExtension))]
    internal class ExampleProjectExtension : ISharePointProjectExtension
    {
        public void Initialize(ISharePointProjectService projectService)
        {
            projectService.ProjectAdded += projectService_ProjectAdded;
            projectService.ProjectInitialized += projectService_ProjectInitialized;
            projectService.ProjectNameChanged += projectService_ProjectNameChanged;
            projectService.ProjectPropertyChanged += projectService_ProjectPropertyChanged;
            projectService.ProjectRemoved += projectService_ProjectRemoved;
            projectService.ProjectDisposing += projectService_ProjectDisposing;
        }

        // A project was added.
        void projectService_ProjectAdded(object sender, SharePointProjectEventArgs e)
        {
            ISharePointProject project = (ISharePointProject)sender;
            string message = String.Format("The following project was added: {0}", e.Project.Name);
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }

        // A project is loaded in the IDE.
        void projectService_ProjectInitialized(object sender, SharePointProjectEventArgs e)
        {
            ISharePointProject project = (ISharePointProject)sender;
            string message = String.Format("The following project is being initialized: {0}", e.Project.Name);
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }

        // The name of a project was changed.
        void projectService_ProjectNameChanged(object sender, NameChangedEventArgs e)
        {
            ISharePointProject project = (ISharePointProject)sender;
            string message = String.Format("The project named {0} was changed to {1}.", e.OldName, project.Name);
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }

        // A project property value was changed.
        private void projectService_ProjectPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ISharePointProject project = (ISharePointProject)sender;
            string message = String.Format("The following property of the {0} project was changed: {1}", 
                project.Name, e.PropertyName);
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }

        // A project is being removed or unloaded.
        void projectService_ProjectRemoved(object sender, SharePointProjectEventArgs e)
        {
            ISharePointProject project = (ISharePointProject)sender;
            string message = String.Format("The following project is being removed or unloaded: {0}", e.Project.Name);
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }

        // A project was removed or unloaded.
        void projectService_ProjectDisposing(object sender, SharePointProjectEventArgs e)
        {
            ISharePointProject project = (ISharePointProject)sender;
            string message = String.Format("The following project was removed or unloaded: {0}", e.Project.Name);
            project.ProjectService.Logger.WriteLine(message, LogCategory.Message);
        }
    }
}

Cet exemple utilise le service de projet SharePoint pour écrire le message dans les fenêtres Sortie et Liste d'erreurs. Pour plus d'informations, consultez Utilisation du service de projet SharePoint.

Pour obtenir des exemples illustrant la gestion des événements ProjectMenuItemsRequested et ProjectPropertiesRequested, consultez Comment : ajouter un élément de menu contextuel à des projets SharePoint et Comment : ajouter une propriété à des projets SharePoint.

Compilation du code

Cet exemple nécessite des références aux assemblys suivants :

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Déploiement de l'extension

Pour déployer l'extension, créez un package d'extension Visual Studio (VSIX) pour l'assembly et tous les autres fichiers que vous voulez distribuer avec l'extension. Pour plus d'informations, consultez Déploiement d'extensions pour les outils SharePoint dans Visual Studio.

Voir aussi

Concepts

Extension du système de projet SharePoint

Autres ressources

Comment : ajouter un élément de menu contextuel à des projets SharePoint

Comment : ajouter une propriété à des projets SharePoint

Procédure pas à pas : création d'une extension de projet SharePoint