Partilhar via


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 está 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 à classe.Este atributo permite que Visual Studio descobrir e carregar seu ISharePointProjectExtension implementação.Passar o ISharePointProjectExtension tipo para o construtor de atributo.

  5. Na sua implementação da Initialize método, o uso de membros da projectService parâmetro para definir o comportamento de sua extensão.Este parâmetro é um ISharePointProjectService que fornece acesso para os eventos definidos no objeto do 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 que são definidos pelo ISharePointProjectEvents interface.Para testar o código, crie um projeto do SharePoint na Visual Studio e, em seguida, adicionar mais projetos à solução, alterar os valores de propriedade do projeto, ou excluir ou excluir um projeto.A extensão de notificá-lo sobre os eventos escrevendo 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: adiciona 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 (VSIX) da extensão do pacote para o assembly e outros arquivos que você deseja distribuir com a extensão.Para obter mais informações, consulte Implantação de extensões para as ferramentas de SharePoint no Visual Studio.

Consulte também

Tarefas

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

Conceitos

Estendendo o sistema de projeto do SharePoint

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

Como: adicionar uma propriedade para projetos do SharePoint