How to get all of the deactivated Features in the farm that could be activated for the specific web using Sharepoint Server Object Model?

656 views Asked by At

I need to get list of SPFeatureDefinitions like in ManageFeatures.aspx page. Probably it should be smth like this:

...
using (SPWeb web = spSite.OpenWeb())
foreach (var spfeature in SPFarm.Local.FeatureDefinitions)
{
    result.Add(spfeature);                           
}

But how can I be sure that spfeature could be activated on web?

2

There are 2 answers

0
Rick Mayo On BEST ANSWER

try using if (spfeature.Scope = SPFeatureScope.Web)

0
Roman.Tkachenko On

Figured it out. I have to get all feature definitions and then filter them by web scope. Here is the code:

using (SPSite spSite = new SPSite(siteId))
{
    using (SPWeb web = spSite.OpenWeb(selectedWeb.Id))
    {
        foreach (var spfeature in SPFarm.Local.FeatureDefinitions)
        {
            if (spfeature.Scope.Equals(SPFeatureScope.Web) && !spfeature.Hidden)
            {
                var feature = new Feature();
                feature.Name = spfeature.DisplayName;
                feature.Id = spfeature.Id;
                feature.IsActive = web.Features[spfeature.Id] != null;

                result.Add(feature);
            }                            
        }
    }
}