Dennis G
Dennis G

Reputation: 21788

How to check whether a feature has been activated?

I know I can check the installed activated (read what Kyle said below) features of a site via SPSite.Features.

I also know I can add or remove a feature via spSite.Features.Add("featureId") or .Remove.

The question is: How do I check whether a feature is active? When querying SPSite.Features, I get all the features for the site, it returns SPFeature objects. But I still don't know if the feature is active or not.

Basically I would like to have a bool of spSite.Features["featureId"].isActive or something similar.

Upvotes: 5

Views: 20356

Answers (1)

Kyle Trauberman
Kyle Trauberman

Reputation: 25684

SPSite.Features doesn't contain installed features. It contains activated features.

To grab a list of all features that are installed, whether activated or not, you need to grab SPFeatureDefinition objects from the SPSite.FeatureDefinitions property.

// Get a list of activated features
SPFeatureCollection features = SPContext.Current.Site.Features;

// Get a list of all feature definitions available
SPFeatureDefinitionCollection featureDefinitions = SPContext.Current.Site.FeatureDefinitions;

A better description from msdn:

The presence of a feature in a collection at the farm 
(Microsoft.SharePoint.Administration.SPWebService), Web application
(Microsoft.SharePoint.Administration.SPWebApplication), site collection
([T:Microsoft.SharePoint.SPSite)], or Web site (Microsoft.SharePoint.SPWeb) 
levels indicates that the feature is activated. Lack of an SPFeature object 
indicates that the object is not active in the given scope.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.featuredefinitions.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsite.features.aspx

Upvotes: 13

Related Questions