Udostępnij za pośrednictwem


Jak: tworzenie rozszerzenie projektu programu SharePoint

Rozszerzenie projektu należy utworzyć, gdy chce się dodać funkcje do jakiegokolwiek projektu programu SharePoint, która jest otwarta w programie Visual Studio.Aby uzyskać więcej informacji, zobacz Rozszerzenia systemu projektu programu SharePoint.

Aby utworzyć rozszerzenie projektu

  1. Utwórz projekt biblioteki klas.

  2. Dodaj odwołania do następujących zestawów:

    • Microsoft.VisualStudio.SharePoint

    • System.ComponentModel.Composition

  3. Tworzenie klasy implementującej ISharePointProjectExtension interfejsu.

  4. Dodaj System.ComponentModel.Composition.ExportAttribute do klasy.Atrybut ten umożliwia wykrywanie i załadować do programu Visual Studio na ISharePointProjectExtension wdrożenia.Przekazać ISharePointProjectExtension typu Konstruktor atrybutu.

  5. W implementacji Initialize metodę, użyj członków projectService parametr definiują zachowanie programu rozszerzenia.Ten parametr jest ISharePointProjectService obiekt, który zapewnia dostęp do zdarzenia zdefiniowane w ISharePointProjectEvents interfejsu.

Przykład

Poniższy przykład kodu demonstruje, jak utworzyć rozszerzenie prosty projekt, który obsługuje większość zdarzeń projektu programu SharePoint, które są definiowane przez ISharePointProjectEvents interfejsu.Aby przetestować kod, należy utworzyć projekt programu SharePoint w Visual Studio i następnie dodać więcej projektów do roztworu, zmiany wartości właściwości projektu, usunąć lub wyłączyć projektu.Rozszerzenie powiadamia go zdarzenia przez pisanie wiadomości do danych wyjściowych okna i Listy błędów okna.

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

W tym przykładzie użyto usługi projektu programu SharePoint, aby napisać wiadomość do produkcji okna i Listy błędów okna.Aby uzyskać więcej informacji, zobacz Za pomocą usługi SharePoint projektu.

Przykłady ilustrujące sposób obsługi ProjectMenuItemsRequested i ProjectPropertiesRequested zdarzeń, zobacz Jak: Dodawanie elementu Menu skrótów do projektów programu SharePoint i Jak: Dodawanie właściwości do projektów programu SharePoint.

Kompilowanie kodu

W tym przykładzie wymaga odwołania do następujących zestawów:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

Wdrażanie rozszerzenia

Aby wdrożyć rozszerzenia, tworzenie Visual Studio extension (VSIX) pakiet Zgromadzenia i inne pliki, które chcesz dystrybuować z rozszerzeniem.Aby uzyskać więcej informacji, zobacz Wdrażanie rozszerzeń dla narzędzia programu SharePoint w programie Visual Studio.

Zobacz też

Zadania

Instruktaż: Tworzenie rozszerzenie projektu programu SharePoint

Koncepcje

Rozszerzenia systemu projektu programu SharePoint

Jak: Dodawanie elementu Menu skrótów do projektów programu SharePoint

Jak: Dodawanie właściwości do projektów programu SharePoint