One of the tasks I needed to do for my upgrade was to convert a sub site to a site collection. This is something I’m still trying to get right as of this writing but figured I’d share a new command I had to create to help with that. To convert a site to a site collection at a managed path you need to create the managed path, create a site with no site template, export your existing site, and then import to the new location all using STSADM. Before you can do the import though you have to make sure that certain required features are enabled.
I found that STSADM had the ability to activate and deactivate features but there was no way for me to tell which features were currently set (without using the web tool). I wanted/needed an easy way to identify existing features associated with a site collection as well as what the actual name is (if you’ve ever tried to match the descriptive name to the feature name it’s not always obvious). So my solution was to create a simple command which would list out all features along with their actual name (not the user friendly name).
I snagged most of the code from the Microsoft.SharePoint.WebControls.FeatureActivator server control in the Microsoft.SharePoint.ApplicationPages assembly. This is the main control that SharePoint uses when you wish to activate or deactivate features on a site (or farm or web application). The long and short of it is that I’m building a DataTable of features by looping through a SPFeatureDefinitionCollection which I get by calling SPFarm.Local.FeatureDefinitions. Beyond that I’m merely filtering based on passed in parameters. In order to show which features are active at the given scope I check for existence in the active features collection which I get by calling either SPSite’s Features property, SPWeb’s Features property or SPSite’s WebApplication.Features property. I then either return it as XML or a flat list based on a passed in parameter. The core code is shown below:
The syntax of the command can be seen below:
C:\>stsadm -help gl-enumfeatures stsadm -o gl-enumfeatures Returns the list of features and their activation status. Parameters: -url <site collection url> [-scope <Farm | Site | Web | WebApplication>] [-showhidden] [-xml]
Here’s an example of how to return the features for a publishing site site collection at the managed path "/hr":
stsadm –o gl-enumfeatures –url "http://intranet/hr/Pages/" -scope Site -xml
The results of running the above command can be seen below (note that the node names are somewhat confusing as the node DisplayName implies that this is what you would see in the web browser – in reality it’s the Title node that shows in the browser – I thought about changing the name to something else but in the end I decided to be consistent with the property names on the objects):