Share via


SPFeature class

Represents the state of a feature at its corresponding scope.

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPFeature

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public NotInheritable Class SPFeature
'Usage
Dim instance As SPFeature
public sealed class SPFeature

Remarks

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.

Examples

The following code example installs a Feature in the server farm and activates it in a specified Web site.

This example requires using directives (Imports in Visual Basic) for the Microsoft.SharePoint and Microsoft.SharePoint.Utilities namespaces.

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>");
Dim oCultureInfo As New System.Globalization.CultureInfo(1033)
Dim collFeatureDefinitions As SPFeatureDefinitionCollection = SPFarm.Local.FeatureDefinitions

Dim guidFeatureID As New Guid("3C4CD219-D8A7-4fef-BED4-ABA672F7A21A")

Dim oFeatureDefinition As SPFeatureDefinition = collFeatureDefinitions.Add("Feature_Folder\feature.xml", guidFeatureID)

Response.Write(SPEncode.HtmlEncode(oFeatureDefinition.GetTitle(oCultureInfo)) & " installed.<BR>")

Dim collFeatures As SPFeatureCollection = SPContext.Current.Site.AllWebs("Site").Features
Dim oFeature As SPFeature = collFeatures.Add(guidFeatureID)

Response.Write(SPEncode.HtmlEncode(oFeature.Definition.GetTitle(oCultureInfo)) & " activated.<BR>")

Note

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.

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See also

Reference

SPFeature members

Microsoft.SharePoint namespace