This week we have our SharePoint 2007 bootcamp here at U2U with the majority of students coming from outside of Belgium. It is nice to see people crossing the borders and get a taste of Belgium - and they brought the nice weather!
Today I am going to talk about one of my favorite topic: the WSS Features Framework. Think of features as all of the artifacts in sites you can start using: content types, lists, libraries, workflows, enhancements to the UI ("light-up"), event handlers and much more. Administrators can activate or deactivate certain features at the level of the farm, site collection and per individual site. They do this using the Site Features page for example.
When features - such as lists or libraries - are activated, users can use the artifacts in their sites. You can for example allow or disallow users to create instances of the slide library on the team sites.
Activating and deactivating features can be done using the command-line utility called STSADM.EXE and when the feature is not hidden from the UI, you can also use the Site Features page. But there is another approach for activating and deactivating features. You can also do it using the rich object model of WSS. Advantage of doing it this way is that you are able to work at a finer level and you also get to see the ones that are hidden from the UI.
I have create a small demo application that illustrates this. It is available for download from here. Do know again that this is demo-ware :)
You type in the URL to a site and it shows you all of the features currently active and the ones that are available for activation. So what you can do is for example deactivate the Custom List feature preventing the user from creating their own custom lists and only use the template lists (I know it overstretched but it is an example).

In the bottom list you see all of the features installed and candidates for activation on the site.With an simple click, you can activate the Custom List again.
What about the code?
Listing all of the activated features and the ones that are candidates can be done like this. Note that you have a new SPFarm class within the object model giving you access to the features that are not activated on the site.
//-- connect to the site
site = new SPSite(textBoxURL.Text);
web = site.OpenWeb();
//-- display all features
listBoxActivateFeatures.Items.Clear();
listBoxDeactivatedFeatures.Items.Clear();
listBoxActivateFeatures.DisplayMember = "DisplayName";
listBoxDeactivatedFeatures.DisplayMember = "DisplayName";
foreach (SPFeature feature in web.Features)
{
listBoxActivateFeatures.Items.Add(feature.Definition);
}
foreach (SPFeatureDefinition feature in SPFarm.Local.FeatureDefinitions)
{
if ((feature.Scope == SPFeatureScope.Web) && (!listBoxActivateFeatures.Items.Contains(feature)))
listBoxDeactivatedFeatures.Items.Add(feature);
}
Activating a feature can be done like this:
SPFeatureDefinition feature = (SPFeatureDefinition)listBoxDeactivatedFeatures.SelectedItem;
web.Features.Add(feature.Id);
Deactivating a feature can be done like this:
SPFeatureDefinition feature = (SPFeatureDefinition)listBoxActivateFeatures.SelectedItem;
web.Features.Remove(feature.Id);
Very powerful stuff if you ask me. Enjoy the experimentation!