Como: Criar uma extensão de projeto do SharePoint

Crie uma extensão de projeto quando quiser adicionar funcionalidade a qualquer projeto do SharePoint que é aberto no Visual Studio. Para obter mais informações, consulte Estendendo o sistema de projeto do SharePoint.

Para criar uma extensão de projeto

  1. Crie um projeto de biblioteca de classe.

  2. Adicione referências para os seguintes conjuntos de módulos (assemblies) :

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. Crie uma nova classe que implemente a interface ISharePointProjectExtension.

  4. Adicionar o System.ComponentModel.Composition.ExportAttribute para a classe. Este atributo permite Visual Studio descobrir e carregar o ISharePointProjectExtension de implementação. Passar o ISharePointProjectExtension o tipo para o construtor de atributo.

  5. Na implementação da Initialize método, use os membros a projectService parâmetro para definir o comportamento de extensão. Este parâmetro é um ISharePointProjectService objeto fornece acesso a eventos definidos no ISharePointProjectEvents interface.

Exemplo

O exemplo de código a seguir demonstra como criar uma extensão de projeto simples que lida com a maioria dos eventos de projeto do SharePoint são definidos pelo ISharePointProjectEvents interface. Para testar o código, crie um projeto do SharePoint em Visual Studio e adicionar mais projetos à solução, ou alterar os valores de propriedade do projeto, excluir ou excluir um projeto. A extensão notifica os eventos por escrever mensagens para o saída janela e Error List janela.

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

Este exemplo usa o serviço de projeto do SharePoint para gravar a mensagem para o saída janela e Error List janela. Para obter mais informações, consulte Usando o serviço de projeto do SharePoint.

Para obter exemplos que demonstram como lidar com o ProjectMenuItemsRequested e ProjectPropertiesRequested eventos, consulte Como: Adicionar um Item de Menu de atalho para projetos do SharePoint e Como: Adicionar uma propriedade para projetos do SharePoint.

Compilando o código

Este exemplo requer referências aos assemblies seguintes:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Implantação da extensão

Para implantar a extensão, criar um Visual Studio pacote de extensão (VSIX) para o assembly e outros arquivos que você deseja distribuir com extensão. Para obter mais informações, consulte Implantar extensões para as ferramentas do SharePoint em Visual Studio.

Consulte também

Conceitos

Estendendo o sistema de projeto do SharePoint

Outros recursos

Como: Adicionar um Item de Menu de atalho para projetos do SharePoint

Como: Adicionar uma propriedade para projetos do SharePoint

Demonstra Passo a passo: Criando uma extensão de projeto do SharePoint