Jaa


How to: Get data for a built-in SharePoint node in Server Explorer

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

For each built-in SharePoint node in Server Explorer, you can get data for the underlying SharePoint component that the node represents. For more information, see Extend the SharePoint connections node in Server Explorer.

Example

The following code example demonstrates how to get data for the underlying SharePoint list that a list node represents in Server Explorer. By default, list nodes have a View in Browser context menu item that you can click to open the lists in a Web browser. This example extends list nodes by adding a View in Visual Studio context menu item that opens the lists directly in Visual Studio. The code accesses the list data for the node to get the URL of the list to open in Visual Studio.

Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint
Imports Microsoft.VisualStudio.SharePoint.Explorer
Imports Microsoft.VisualStudio.SharePoint.Explorer.Extensions

Namespace Contoso.ServerExplorerExtension
    <Export(GetType(IExplorerNodeTypeExtension))> _
    <ExplorerNodeType(ExtensionNodeTypes.ListNode)> _
    Friend Class ListNodeExtension
        Implements IExplorerNodeTypeExtension

        Private projectService As ISharePointProjectService
        Private dteObject As EnvDTE.DTE

        Private Sub Initialize(ByVal nodeType As IExplorerNodeType) _
            Implements IExplorerNodeTypeExtension.Initialize
            AddHandler nodeType.NodeMenuItemsRequested, AddressOf NodeMenuItemsRequested
        End Sub

        Private Sub NodeMenuItemsRequested(ByVal Sender As Object, ByVal e As ExplorerNodeMenuItemsRequestedEventArgs)
            Dim menuItem = e.MenuItems.Add("View in Visual Studio")
            AddHandler menuItem.Click, AddressOf MenuItemClick
        End Sub

        Private Sub MenuItemClick(ByVal Sender As Object, ByVal e As MenuItemEventArgs)

            ' Get the data for the list node.
            Dim node As IExplorerNode = CType(e.Owner, IExplorerNode)
            Dim nodeInfo As IListNodeInfo = node.Annotations.GetValue(Of IListNodeInfo)()

            If dteObject Is Nothing Then

                If projectService Is Nothing Then
                    projectService = CType(node.ServiceProvider.GetService(GetType(ISharePointProjectService)), 
                        ISharePointProjectService)
                End If
                dteObject = CType(projectService.ServiceProvider.GetService(GetType(EnvDTE.DTE)), EnvDTE.DTE)
            End If

            dteObject.ItemOperations.Navigate(nodeInfo.DefaultViewUrl.ToString(),
                EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow)
        End Sub
    End Class
End Namespace
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;
using Microsoft.VisualStudio.SharePoint.Explorer.Extensions;

namespace Contoso.ServerExplorerExtension
{
    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExtensionNodeTypes.ListNode)]
    internal class ListNodeExtension : IExplorerNodeTypeExtension
    {
        private ISharePointProjectService projectService;
        private EnvDTE.DTE dteObject;

        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeMenuItemsRequested += nodeType_NodeMenuItemsRequested;
        }

        void nodeType_NodeMenuItemsRequested(object sender, ExplorerNodeMenuItemsRequestedEventArgs e)
        {
            IMenuItem menuItem = e.MenuItems.Add("View in Visual Studio");
            menuItem.Click += menuItem_Click;
        }

        void menuItem_Click(object sender, MenuItemEventArgs e)
        {
            // Get the data for the list node.
            IExplorerNode node = (IExplorerNode)e.Owner;
            IListNodeInfo nodeInfo = node.Annotations.GetValue<IListNodeInfo>();

            if (dteObject == null)
            {
                if (projectService == null)
                {
                    projectService = (ISharePointProjectService)node.ServiceProvider.GetService(
                        typeof(ISharePointProjectService));
                }

                dteObject = (EnvDTE.DTE)projectService.ServiceProvider.GetService(typeof(EnvDTE.DTE));
            }

            dteObject.ItemOperations.Navigate(nodeInfo.DefaultViewUrl.ToString(), 
                EnvDTE.vsNavigateOptions.vsNavigateOptionsNewWindow);
        }
    }
}

This example uses the SharePoint project service to obtain the DTE object that is used to open lists in Visual Studio. For more information about the SharePoint project service, see Use the SharePoint project service.

For more information about the basic tasks to create an extension for a SharePoint node, see How to: Extend a SharePoint node in Server Explorer.

Compile the code

This example requires references to the following assemblies:

  • EnvDTE

  • Microsoft.VisualStudio.SharePoint

  • Microsoft.VisualStudio.SharePoint.Explorer.Extensions

  • System.ComponentModel.Composition

Deploy the extension

To deploy the Server Explorer extension, create a Visual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. For more information, see Deploy extensions for the SharePoint tools in Visual Studio.

See also