Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to: SharePoint Foundation 2010
Microsoft SharePoint Foundation offers a full object model for discovering the list of installed features within a given scope, and for controlling whether features are enabled at the farm and site levels.
Microsoft.SharePoint.SPFeature (SPFeatureCollection) Returns an object that represents the state of a feature at its corresponding level. The presence of a feature in a collection at the farm (Microsoft.SharePoint.Administration.SPWebService), Web application (Microsoft.SharePoint.Administration.SPWebApplication), site collection ([T:Microsoft.SharePoint.SPSite)], or Web site (Microsoft.SharePoint.SPWeb) levels indicates that the feature is activated. Lack of an SPFeature object indicates that the object is not active in the given scope. Namespace: Microsoft.SharePoint
Microsoft.SharePoint.SPFeatureProperty (SPFeaturePropertyCollection) Represents a single feature property. Namespace: Microsoft.SharePoint
Microsoft.SharePoint.SPFeatureScope An enumeration of the possible scopes that can be specified for a feature, including Farm, WebApplication, Site, and Web. Namespace: Microsoft.SharePoint
Microsoft.SharePoint.Administration.SPFeatureDefinition (SPFeatureDefinitionCollection) Contains the base definition of a feature, including the name, scope, ID, and version of the feature. Also, globally per feature, you can store properties about the feature. Namespace: Microsoft.SharePoint.Administration
Microsoft.SharePoint.SPFeatureDependency (SPFeatureDependencyCollection) Represents a feature that is depended upon by another feature. Namespace: Microsoft.SharePoint
Microsoft.SharePoint.Administration.SPElementDefinition (SPElementDefinitionCollection) Represents an element to be provisioned when the feature is activated or used. Namespace: Microsoft.SharePoint.Administration
Get the collection of features for a farm, SharePoint Foundation Web application, site collection, or Web site by using one of the following properties to access the collection:
SPWebApplication.Features Returns a list of activated virtual server-scoped features for the SharePoint Foundation Web application.
SPWebService.Features Returns the administrative features that have been activated at the server farm scope.
SPFarm.FeatureDefinitions Returns the list of all installed features in the server farm.
SPSite.Features Returns the list of activated features for the site collection.
SPWeb.Features Returns the list of activated features for a site.
SPFeatureDefinition.ActivationDependencies Returns the list of features upon which activation of another feature depends.
QueryFeatures(Guid) Returns a list of all of the site collection-scoped and Web site-scoped features of the given GUID.
The following example displays the list of names and the GUIDs of all the features that are activated on a specified site:
SPSite siteCollection = SPControl.GetContextSite(Context);
SPWeb site = siteCollection.AllWebs["Site"];
SPFeatureCollection siteFeatures = site.Features;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo.GetCultureInfo("en-US");
foreach (SPFeature siteFeature in siteFeatures)
{
Response.Write("Title: " + siteFeature.Definition.GetTitle(cultureInfo) + "<BR>ID:" + siteFeature.DefinitionId.ToString() + "<BR><BR>");
}
The next example uses information returned through the previous example to add a feature to a subsite:
SPWeb subSite = site.Webs["SubSite"];
System.Guid guid = new System.Guid("6e005f62-f8b2-4073-a673-c035c9129946");
subSite.Features.Add(guid);