Cómo: Crear una extensión de proyecto de SharePoint
Cree una extensión de proyecto cuando desee agregar funcionalidad a un proyecto SharePoint que esté abierto en Visual Studio.Para obtener más información, vea Extender el sistema de proyectos de SharePoint.
Para crear una extensión de proyecto
Cree un proyecto de biblioteca de clases
Agregue referencias a los siguientes ensamblados:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
Cree una clase que implemente la interfaz ISharePointProjectExtension.
Agregue System.ComponentModel.Composition.ExportAttribute a la clase.Este atributo permite que Visual Studio detecte y cargue la implementación de ISharePointProjectExtension.Pase el tipo ISharePointProjectExtension al constructor del atributo.
En la implementación del método Initialize, use los miembros del parámetro projectService para definir el comportamiento de la extensión.Este parámetro es un objeto ISharePointProjectService que proporciona acceso a los eventos definidos en la interfaz ISharePointProjectEvents.
Ejemplo
El siguiente ejemplo de código muestra cómo crear una extensión de proyecto simple que controla la mayoría de los eventos de proyecto de SharePoint que define la interfaz ISharePointProjectEvents.Para probar el código, cree un proyecto de SharePoint en Visual Studio y, a continuación, agregue más proyectos a la solución, cambie los valores de propiedad del proyecto, o elimine o excluya un proyecto.La extensión notifica los eventos escribiendo los mensajes en el Ventana de salida y Lista de errores.
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);
}
}
}
En este ejemplo, se usa el servicio de proyecto de SharePoint para escribir el mensaje en la Ventana de salida y en la ventana Lista de errores.Para obtener más información, vea Utilizar el servicio de proyecto de SharePoint.
Para obtener ejemplos que muestran cómo controlar los eventos ProjectMenuItemsRequested y ProjectPropertiesRequested, vea Cómo: Agregar un elemento de menú contextual a los proyectos de SharePoint y Cómo: Agregar una propiedad a proyectos de SharePoint.
Compilar el código
Para este ejemplo se requieren referencias a los siguientes ensamblados:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
Implementar la extensión
Para implementar la extensión, cree un paquete de extensión (VSIX) de Visual Studio para el ensamblado y el resto de archivos que desee distribuir con la extensión.Para obtener más información, vea Extensiones de implementación para las Herramientas de SharePoint en Visual Studio.
Vea también
Tareas
Tutorial: Crear una extensión de proyecto de SharePoint
Conceptos
Extender el sistema de proyectos de SharePoint
Cómo: Agregar un elemento de menú contextual a los proyectos de SharePoint