Export (0) Print
Expand All

How to: Add a Custom SharePoint Node to Server Explorer

You can add custom nodes under the SharePoint Connections node in Server Explorer. This is useful when you want to display additional SharePoint components that are not displayed in Server Explorer by default. For more information, see Extending the SharePoint Connections Node in Server Explorer.

To add a custom node, first create a class that defines the new node. Then create an extension that adds the node as a child of an existing node.

To define the new node

  1. Create a class library project.

  2. Add references to the following assemblies:

    • Microsoft.VisualStudio.SharePoint

    • Microsoft.VisualStudio.SharePoint.Explorer.Extensions

    • System.ComponentModel.Composition

    • System.Drawing

  3. Create a class that implements the IExplorerNodeTypeProvider interface.

  4. Add the following attributes to the class:

  5. In your implementation of the IExplorerNodeTypeProvider.InitializeType method, use members of the typeDefinition parameter to configure the behavior of the new node. This parameter is an IExplorerNodeTypeDefinition object that provides access to the events defined in the IExplorerNodeEvents interface.

    The following code example demonstrates how to define a new node. This example assumes that your project contains an icon named CustomChildNodeIcon as an embedded resource.

    [Export(typeof(IExplorerNodeTypeProvider))]
    [ExplorerNodeType(ExampleNodeTypeProvider.NodeTypeId)]
    internal class ExampleNodeTypeProvider : IExplorerNodeTypeProvider
    {
        internal const string NodeTypeId = "Contoso.ServerExplorerNodeExample";
    
        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon =
                Properties.Resources.CustomChildNodeIcon.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;
        }
    }
    

To add the new node as a child of an existing node

  1. In the same project as your node definition, create a class that implements the IExplorerNodeTypeExtension interface.

  2. Add the ExportAttribute attribute to the class. This attribute enables Visual Studio to discover and load your IExplorerNodeTypeExtension implementation. Pass the IExplorerNodeTypeExtension type to the attribute constructor.

  3. Add the ExplorerNodeTypeAttribute attribute to the class. In a node extension, this attribute specifies the string identifier for the type of node that you want to extend.

    To specify built-in node types provided by Visual Studio, pass one of the following enumeration values to the attribute constructor:

    • ExplorerNodeTypes: Use these values to specify site connection nodes (the nodes that display site URLs), site nodes, or all other parent nodes in Server Explorer.

    • ExtensionNodeTypes: Use these values to specify one of the built-in nodes that represent an individual component on a SharePoint site, such as a node that represents a list, field, or content type.

  4. In your implementation of the Initialize method, handle the NodeChildrenRequested event of the IExplorerNodeType parameter.

  5. In the NodeChildrenRequested event handler, add the new node to the child nodes collection of the Node object that is exposed by the event arguments parameter.

    The following code example demonstrates how to add the new node as a child of the SharePoint site node in Server Explorer.

    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    internal class SiteNodeExtension : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }
    
        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            e.Node.ChildNodes.Add(ExampleNodeTypeProvider.NodeTypeId,
                "Custom Node", null);
        }
    }
    

The following code example provides the complete code to define a simple node and add it as a child of the SharePoint site node in Server Explorer.

using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;
using Microsoft.VisualStudio.SharePoint.Explorer;

namespace Contoso.ServerExplorerExtension
{
    [Export(typeof(IExplorerNodeTypeProvider))]
    [ExplorerNodeType(ExampleNodeTypeProvider.NodeTypeId)]
    internal class ExampleNodeTypeProvider : IExplorerNodeTypeProvider
    {
        internal const string NodeTypeId = "Contoso.ServerExplorerNodeExample";

        public void InitializeType(IExplorerNodeTypeDefinition typeDefinition)
        {
            typeDefinition.DefaultIcon =
                Properties.Resources.CustomChildNodeIcon.ToBitmap();
            typeDefinition.IsAlwaysLeaf = true;
        }
    }

    [Export(typeof(IExplorerNodeTypeExtension))]
    [ExplorerNodeType(ExplorerNodeTypes.SiteNode)]
    internal class SiteNodeExtension : IExplorerNodeTypeExtension
    {
        public void Initialize(IExplorerNodeType nodeType)
        {
            nodeType.NodeChildrenRequested += NodeChildrenRequested;
        }

        private void NodeChildrenRequested(object sender, ExplorerNodeEventArgs e)
        {
            e.Node.ChildNodes.Add(ExampleNodeTypeProvider.NodeTypeId,
                "Custom Node", null);
        }
    }
}

This example assumes that your project contains an icon named CustomChildNodeIcon as an embedded resource. This example also requires references to the following assemblies:

  • Microsoft.VisualStudio.SharePoint

  • System.ComponentModel.Composition

  • System.Drawing

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.

Show:
© 2015 Microsoft