Partager via


How to: Retrieve the SharePoint project service

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can access the SharePoint project service in the following types of solutions:

  • An extension of the SharePoint project system, such as a project extension, project item extension, or project item type definition. For more information about these types of extensions, see Extend the SharePoint project system.

  • An extension of the SharePoint Connections node in Server Explorer. For more information about these types of extensions, see Extend the SharePoint connections node in Server Explorer.

  • Another type of Visual Studio extension, such as a VSPackage.

Retrieve the service in project system extensions

In any extension of the SharePoint project system, you can access the project service by using the ProjectService property of an ISharePointProject object.

You can also retrieve the project service by using the following procedures.

To retrieve the service in a project extension

  1. In your implementation of the ISharePointProjectExtension interface, locate the Initialize method.

  2. Use the projectService parameter to access the service.

    The following code example demonstrates how to use the project service to write a message to the Output window and Error List window in a simple project extension.

    <Export(GetType(ISharePointProjectExtension))> _
    Friend Class GetServiceInProject
        Implements ISharePointProjectExtension
    
        Private Sub Initialize(ByVal projectService As ISharePointProjectService) _
            Implements ISharePointProjectExtension.Initialize
            projectService.Logger.WriteLine("This message was written by using the " & _
                "project service in a project extension.", LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(ISharePointProjectExtension))]
    internal class GetServiceInProject : ISharePointProjectExtension
    {
        public void Initialize(ISharePointProjectService projectService)
        {
            projectService.Logger.WriteLine("This message was written by using the " +
                "project service in a project extension.", LogCategory.Message);
        }
    }
    

    For more information about creating project extensions, see How to: Create a SharePoint project extension.

To retrieve the service in a project item extension

  1. In your implementation of the ISharePointProjectItemTypeExtension interface, locate the Initialize method.

  2. Use the ProjectService property of the projectItemType parameter to retrieve the service.

    The following code example demonstrates how to use the project service to write a message to the Output window and Error List window in a simple extension of the List Definition project item.

    <Export(GetType(ISharePointProjectItemTypeExtension))> _
    <SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")> _
    Friend Class GetServiceInProjectItem
        Implements ISharePointProjectItemTypeExtension
    
        Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
            Implements ISharePointProjectItemTypeExtension.Initialize
            projectItemType.ProjectService.Logger.WriteLine("This message was written " & _
                "by using the project service in an extension for the ListDefinition project item.", _
                LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.ListDefinition")]
    internal class GetServiceInProjectItem : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectService.Logger.WriteLine("This message was written " +
                "by using the project service in an extension for the ListDefinition project item.", 
                LogCategory.Message);
        }
    }
    

    For more information about creating project item extensions, see How to: Create a SharePoint project item extension.

To retrieve the service in a project item type definition

  1. In your implementation of the ISharePointProjectItemTypeProvider interface, locate the InitializeType method.

  2. Use the ProjectService property of the typeDefinition parameter to retrieve the service.

    The following code example demonstrates how to use the project service to write a message to the Output window and Error List window in a simple project item type definition.

    <Export(GetType(ISharePointProjectItemTypeProvider))> _
    <SharePointProjectItemType("Contoso.CustomAction")> _
    Friend Class CustomActionProvider
        Implements ISharePointProjectItemTypeProvider
    
        Private Sub InitializeType(ByVal projectItemTypeDefinition As ISharePointProjectItemTypeDefinition) _
            Implements ISharePointProjectItemTypeProvider.InitializeType
            projectItemTypeDefinition.ProjectService.Logger.WriteLine("This message was written " & _
                "by using the project service in the Custom Action project item type.", _
                LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(ISharePointProjectItemTypeProvider))]
    [SharePointProjectItemType("Contoso.CustomAction")]
    internal class CustomActionProvider : ISharePointProjectItemTypeProvider
    {
        public void InitializeType(ISharePointProjectItemTypeDefinition projectItemTypeDefinition)
        {
            projectItemTypeDefinition.ProjectService.Logger.WriteLine("This message was written " +
                "by using the project service in the Custom Action project item type definition.",
                LogCategory.Message);
        }
    }
    

    For more information about defining project item types, see How to: Define a SharePoint project item type.

Retrieve the service in Server Explorer extensions

In an extension of the SharePoint Connections node in Server Explorer, you can access the project service by using the ServiceProvider property of an IExplorerNode object.

To retrieve the service in a Server Explorer extension

  1. Get an IServiceProvider object from the ServiceProvider property of an IExplorerNode object in your extension.

  2. Use the GetService method to request an ISharePointProjectService object.

    The following code example demonstrates how to use the project service to write a message to the Output window and Error List window from a shortcut menu that the extension adds to list nodes in Server Explorer.

    <Export(GetType(IExplorerNodeTypeExtension))> _
    <ExplorerNodeType(ExtensionNodeTypes.ListNode)> _
    Friend Class ListNodeExtension
        Implements IExplorerNodeTypeExtension
    
        Private projectService As ISharePointProjectService
    
        Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
            Implements IExplorerNodeTypeExtension.Initialize
            AddHandler nodeType.NodeMenuItemsRequested, AddressOf nodeType_NodeMenuItemsRequested
        End Sub
    
        Private Sub nodeType_NodeMenuItemsRequested(ByVal Sender As Object, ByVal e As ExplorerNodeMenuItemsRequestedEventArgs)
            Dim writeMessageMenuItem As IMenuItem = e.MenuItems.Add("Write Message to Output Window and Error List Window")
            AddHandler writeMessageMenuItem.Click, AddressOf writeMessageMenuItem_Click
        End Sub
    
        Private Sub writeMessageMenuItem_Click(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
            Dim node As IExplorerNode = CType(e.Owner, IExplorerNode)
            If projectService Is Nothing Then
                projectService = CType(node.ServiceProvider.GetService(GetType(ISharePointProjectService)), ISharePointProjectService)
            End If
            projectService.Logger.WriteLine("Clicked the menu item for " + node.Text, LogCategory.Message)
        End Sub
    End Class
    
    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExtensionNodeTypes.ListNode)]
    internal class ListNodeExtension : IExplorerNodeTypeExtension
    {
        private ISharePointProjectService projectService;
    
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeMenuItemsRequested += nodeType_NodeMenuItemsRequested;
        }
    
        void nodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            IMenuItem writeMessageMenuItem = e.MenuItems.Add("Write Message to Output Window and Error List Window");
            writeMessageMenuItem.Click += writeMessageMenuItem_Click;
        }
    
        void writeMessageMenuItem_Click(object sender, MenuItemEventArgs e)
        {
            IExplorerNode node = (IExplorerNode)e.Owner;
            if (projectService == null)
            {
                projectService = (ISharePointProjectService)node.ServiceProvider.GetService(typeof(ISharePointProjectService));
            }
    
            projectService.Logger.WriteLine("Clicked the menu item for " + node.Text, LogCategory.Message);
        }
    }
    

    For more information about extending the SharePoint Connections node in Server Explorer, see How to: Extend a SharePoint node in Server Explorer.

Retrieve the Service in other Visual Studio extensions

You can retrieve the project service in a VSPackage, or in any Visual Studio extension that has access to a DTE2 object in the automation object model, such as a project template wizard that implements the IWizard interface.

In a VSPackage, you can request an ISharePointProjectService object by using one of the following methods:

See also