Windows SharePoint Services 3.0 provides a complete set of object models to list the feature installed in a given scope and to control whether feature is available within that server farm or at the site level.
Feature Class Library
Microsoft.SharePoint.SPFeature (Spfeaturecollection) Returns an object that describes the state of feature in the corresponding layer. Presence in the feature collection (Microsoft.SharePoint.Administration.SPWebService), the Web application ( Microsoft.SharePoint.Administration.SPWebApplication), site collection (Microsoft.SharePoint.SPSite), or Web site ( MICROSOFT.SHAREPOINT.SPWEB) layer to indicate whether the feature is active. If the missing corresponding I spfeature object in the collection indicates that the feature is not activated within a given range. Namespaces: Microsoft.SharePoint
Microsoft.SharePoint.SPFeatureProperty (Spfeaturepropertycollection) represents the property of a feature. Namespaces: Microsoft.SharePoint
Microsoft.SharePoint.SPFeatureScope an enumeration of possible ranges that can be specified, including Farm, WebApplication, Site, and Web. Namespaces: Microsoft.SharePoint
Microsoft.SharePoint.Administration.SPFeatureDefinition (spfeaturedefinitioncollection) includes a feature base definition, containing a name, Range, ID, version. At the same time, you can store some of the global properties in each featue. Namespaces: Microsoft.SharePoint.Administration
Microsoft.SharePoint.SPFeatureDependency (Spfeaturedependencycollection) represents a dependency relationship with a feature. Namespaces: Microsoft.SharePoint
Microsoft.SharePoint.Administration.SPElementDefinition (spelementdefinitioncollection) Represents some of the elements that are preset when the feature is activated or used. Namespaces: Microsoft.SharePoint.Administration
Accessing the Feature Collection
For a farm, WSS Web application (virtual server), site collection, and feature collection, you can access the collection using one of the following properties:
Microsoft.SharePoint.Administration.SPWebApplication.Features returns a list of feature activated on a virtual server, which is within the scope of the Web application.
Microsoft.SharePoint.Administration.SPWebService.Features returns the feature of the management type within the farm scope.
Microsoft.SharePoint.Administration.SPFarm.FeatureDefinitions returns a list of all feature installed in the server farm.
Microsoft.SharePoint.SPSite.Features returns a feature list of activation status within a site collection.
Microsoft.SharePoint.SPWeb.Features returns a feature list of activation status within the site.
Microsoft.SharePoint.Administration.SPFeatureDefinition.ActivationDependencIEs returns a list of feature that a feature activation dependency.
Example
The following example lists the names and GUIDs of all feature that have been activated in a particular Web site content:
SPSite siteCollection = SPControl.GetContextSite(Context);
SPWeb site = siteCollection.AllWebs["Site"];
SPFeatureCollection siteFeatures = site.Features;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(1033);
foreach (SPFeature siteFeature in siteFeatures)
{
Response.Write("Title: " + siteFeature.Definition.GetTitle(cultureInfo) + "<BR>ID:"
+ siteFeature.DefinitionId.ToString() + "<BR><BR>");
}
The following example uses the information returned above to add feature to a subsite:
SPWeb subSite = site.Webs["SubSite"];
System.Guid guid = new System.Guid("6e005f62-f8b2-4073-a673-c035c9129946");
subSite.Features.Add(guid);