Represents the state of a feature at its corresponding scope.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
The presence of an SPFeature object in a collection at the server farm, Web application, site collection, or Web site level indicates that the Feature is activated. Lack of an SPFeature object indicates that the object has not been activated.
Use the Features property of the SPWebService, SPWebApplication, SPSite, or SPWeb class to get the collection of Features that are activated in the server farm, Web application, site collection, or Web site. Use the SiteFeatures or WebFeatures property of the SPContext class to get the collection of Features for the current site collection or site.
Use an indexer to return a single Feature from the collection. For example, if the collection is assigned to a variable named collFeatures, use collFeatures[index] in C#, or collFeatures(index) in Visual Basic, where index is the GUID of the feature.
The following code example installs a Feature in the server farm and activates it in a specified Web site.
System.Globalization.CultureInfo oCultureInfo = new System.Globalization.CultureInfo(1033); SPFeatureDefinitionCollection collFeatureDefinitions = SPFarm.Local.FeatureDefinitions; Guid guidFeatureID = new Guid("3C4CD219-D8A7-4fef-BED4-ABA672F7A21A"); SPFeatureDefinition oFeatureDefinition = collFeatureDefinitions.Add("Feature_Folder\\feature.xml", guidFeatureID); Response.Write(SPEncode.HtmlEncode(oFeatureDefinition.GetTitle(oCultureInfo)) + " installed.<BR>"); SPFeatureCollection collFeatures = SPContext.Current.Site.AllWebs["Site"].Features; SPFeature oFeature = collFeatures.Add(guidFeatureID); Response.Write(SPEncode.HtmlEncode(oFeature.Definition.GetTitle(oCultureInfo)) + " activated.<BR>");
Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Disposing Objects.