This documentation is archived and is not being maintained.

SPFeature Class

Windows SharePoint Services 3

Represents the state of a feature at its corresponding scope.


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

[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class SPFeature

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 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.

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>");

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.

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