Comment : créer une extension de projet SharePointHow to: Create a SharePoint project extension

Créer une extension de projet lorsque vous souhaitez ajouter des fonctionnalités à un projet SharePoint qui est ouvert dans Visual Studio.Create a project extension when you want to add functionality to any SharePoint project that is open in Visual Studio. Pour plus d’informations, consultez étendre le système de projet SharePoint.For more information, see Extend the SharePoint project system.

Pour créer une extension de projetTo create a project extension

  1. Créez un projet de bibliothèque de classes.Create a class library project.

  2. Ajoutez des références aux assemblys suivants :Add references to the following assemblies:

    • Microsoft.VisualStudio.SharePointMicrosoft.VisualStudio.SharePoint

    • System.ComponentModel.CompositionSystem.ComponentModel.Composition

  3. Définissez une classe qui implémente l'interface ISharePointProjectExtension.Create a class that implements the ISharePointProjectExtension interface.

  4. Ajouter le ExportAttribute à la classe.Add the ExportAttribute to the class. Cet attribut permet à Visual Studio détecter et charger votre ISharePointProjectExtension implémentation.This attribute enables Visual Studio to discover and load your ISharePointProjectExtension implementation. Passer le ISharePointProjectExtension type au constructeur d’attribut.Pass the ISharePointProjectExtension type to the attribute constructor.

  5. Dans votre implémentation de la Initialize (méthode), utilisez les membres de la projectService paramètre pour définir le comportement de votre extension.In your implementation of the Initialize method, use members of the projectService parameter to define the behavior of your extension. Ce paramètre est un ISharePointProjectService objet qui fournit l’accès aux événements définis dans le ISharePointProjectEvents interface.This parameter is an ISharePointProjectService object that provides access to the events defined in the ISharePointProjectEvents interface.

ExempleExample

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 qui sont définies par le ISharePointProjectEvents interface.The following code example demonstrates how to create a simple project extension that handles most of the SharePoint project events that are defined by the ISharePointProjectEvents interface. Pour tester le code, créez un projet SharePoint dans Visual StudioVisual Studio puis ajouter plusieurs projets à la solution, modifiez les valeurs de propriété de projet, ou supprimer ou exclure un projet.To test the code, create a SharePoint project in Visual StudioVisual Studio and then add more projects to the solution, change project property values, or delete or exclude a project. L’extension vous informe des événements en écrivant des messages à la sortie fenêtre et liste d’erreurs fenêtre.The extension notifies you of the events by writing messages to the Output window and Error List window.

  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  
  ```  

  ```csharp  
  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 à la sortie fenêtre et liste d’erreurs fenêtre.This example uses the SharePoint project service to write the message to the Output window and Error List window. Pour plus d’informations, consultez utiliser le service de projet SharePoint.For more information, see Use the SharePoint project service.

Pour obtenir des exemples qui montrent comment gérer les ProjectMenuItemsRequested et ProjectPropertiesRequested les événements, consultez Comment : ajouter un élément de menu contextuel à des projets SharePoint et Comment : ajouter une propriété à des projets SharePoint.For examples that demonstrate how to handle the ProjectMenuItemsRequested and ProjectPropertiesRequested events, see How to: Add a shortcut menu item to SharePoint projects and How to: Add a property to SharePoint projects.

Compiler le codeCompile the code

Cet exemple nécessite des références aux assemblys suivants :This example requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePointMicrosoft.VisualStudio.SharePoint

  • System.ComponentModel.CompositionSystem.ComponentModel.Composition

Déployer l’extensionDeploy the extension

Pour déployer l’extension, créez un Visual StudioVisual Studio package d’extension (VSIX) pour l’assembly et tous les autres fichiers que vous souhaitez distribuer avec l’extension.To deploy the extension, create a Visual StudioVisual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. Pour plus d’informations, consultez des outils de déploiement des Extensions pour SharePoint dans Visual Studio.For more information, see Deploy Extensions for the SharePoint tools in Visual Studio.

Voir aussiSee also

Étendre le système de projet SharePoint Extend the SharePoint project system
Comment : ajouter un élément de menu contextuel à des projets SharePoint How to: Add a shortcut menu item to SharePoint projects
Comment : ajouter une propriété à des projets SharePoint How to: Add a property to SharePoint projects
Procédure pas à pas : Créer une extension de projet SharePointWalkthrough: Create a SharePoint project extension