Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SPFeature class

Represents the state of a feature at its corresponding scope.

System.Object
  Microsoft.SharePoint.SPFeature

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
[SubsetCallableTypeAttribute]
public sealed class SPFeature

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.

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

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.