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.

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.

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.

This example requires references to the following assemblies:

  • EnvDTE

  • Microsoft.VisualStudio.SharePoint

  • Microsoft.VisualStudio.SharePoint.Explorer.Extensions

  • System.ComponentModel.Composition

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.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft