Share via


How to: Get Data for a Built-In SharePoint Node in Server Explorer

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 Extending 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 Using 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.

Compiling the Code

This example requires references to the following assemblies:

  • EnvDTE

  • Microsoft.VisualStudio.SharePoint

  • Microsoft.VisualStudio.SharePoint.Explorer.Extensions

  • System.ComponentModel.Composition

Deploying 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 Deploying Extensions for the SharePoint Tools in Visual Studio.

See Also

Concepts

How to: Extend a SharePoint Node in Server Explorer

Using the SharePoint Project Service

Deploying Extensions for the SharePoint Tools in Visual Studio

Other Resources

Extending the SharePoint Connections Node in Server Explorer