Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
The presence of a Feature object in a collection at the server farm, Web application, site collection, or Web site levels 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
] in C#, or
) 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 Best Practices: Using Disposable Windows SharePoint Services Objects.