Extending generic folder nodes – Extending Visual Studio SharePoint development tools tip #4


Generic explorer nodes, like the Features or the Content Types node, weren’t really made to be extended. After all, all they do is to wrap the “real” nodes, so that using the SharePoint Explorer is easier and better performing. Still, there might be situations, when you could want to add some extra functionality to a generic folder node like for example displaying Content Types in groups or grouping Features in Enabled and Disabled.

Extending nodes in SharePoint Explorer

Extending existing nodes in SharePoint Explorer is quite easy. All you have to do is to create a new Visual Studio SharePoint development tools extension and create a class that implements the IExplorerNodeTypeExtension interface.

One of the things that you have to specify, while creating a custom Explorer node extension, is the node type that you are extending, for example a Feature node or a Content Type node. In case of a generic folder node you have to set the ExplorerNodeType attribute to ExplorerNodeTypes.GenericFolderNode:

[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExplorerNodeTypes.GenericFolderNode)]
internal class ContentTypesNodeExtension : IExplorerNodeTypeExtension
{
}

While it works, it extends all generic folder nodes, so eventually your custom functionality will get applied to all container nodes like Content Types, Features etc. There is however a trick…

Extending the right generic folder nodes in SharePoint Explorer

As I already mentioned, the Visual Studio SharePoint development tools API doesn’t provide us with any kind of method to extend a specific generic folder node in the SharePoint Explorer. There is however a trick that you can use, to extend a specific generic folder node in the SharePoint Explorer.

In order to get this done you have to create a node extension, just as if you were extending a specific node type, and apply the modification only when only one child node has been created. Let me show you that in code:

[Export(typeof(IExplorerNodeTypeExtension))]
[ExplorerNodeType(ExtensionNodeTypes.ContentTypeNode)]
internal class ContentTypesNodeExtension : IExplorerNodeTypeExtension
{
    public void Initialize(IExplorerNodeType nodeType)
    {
        nodeType.NodeInitialized += new EventHandler<ExplorerNodeEventArgs>(nodeType_NodeInitialized);
    }

    void nodeType_NodeInitialized(object sender, ExplorerNodeEventArgs e)
    {
        AddGroups(e.Node.ParentNode);
    }

    private void AddGroups(IExplorerNode contentTypesFolder)
    {
        if (contentTypesFolder.ChildNodes != null && contentTypesFolder.ChildNodes.Count() == 1)
        {
            // Apply your extension here
        }
    }
}

First of all we extend the Content Type node type. We initialize the extension just as if we were adding some functionality to every Content Type node in the SharePoint Explorer. But then, when it comes to applying the extension, we use the parent node (which is the Content Types generic folder node) and apply the extension only if the number of child nodes (Content Type nodes) is equal to one, meaning, that the extension will be applied only once.

While it isn’t the best type of extensibility performance-wise (the code will get executed for every Content Type node in the SharePoint Explorer), it is probably the only way in which you can extend a generic folder node being absolutely sure, that the node that you are extending, is the node that you wanted to extend.

Extending generic folder nodes isn’t probably be something that you will be doing very often, but if you will need to do it, the above example might save you some time, trying to figure out how to get it done.

Technorati Tags: SharePoint 2010,Visual Studio 2010

Others found also helpful: