Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 518
Dennis G Avatar asked Mar 02 '11 15:03

Dennis G


1 Answers

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

like image 95
Kyle Trauberman Avatar answered Nov 04 '22 08:11

Kyle Trauberman