Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Farm Features from SharePoint 2010

I am attempting to get a list of Farm Features from a SharePoint 2010 Central Admin site. The issue I am having is that I have only succeeded in pulling back Site Features. The following code is what I am currently working with:

foreach (SPFeature feature in SPAdministrationWebApplication.Local.Features)
{
    string featureName = feature.Definition.DisplayName;
    if (featureName != null)
    {
        XElement newItem = new XElement("Item", featureName);
        infoTree.Add(newItem);
    }

}

I have also tried using SPFarm.Local.FeatureDefinitions as follows:

foreach (SPFeatureDefinition feature in SPFarm.Local.FeatureDefinitions)
{
    string featureName = feature.DisplayName;
if (featureName != null)
    {
        XElement newItem = new XElement("Item", featureName);
        infoTree.Add(newItem);
    }

but to no avail. The next avenue I am approaching is using SPFeatureCollection. Is there a better approach I can take to this issue? Basically I'm just looking for some clues as I have not gotten anything out of SPFeatureCollection just yet.

EDIT I have been messing around with

SPFeatureCollection featureCollect = SPContext.Current.Site.Features  

but so far I am having an issue with SPContext returning null.

like image 242
wjhguitarman Avatar asked Nov 04 '22 14:11

wjhguitarman


1 Answers

I think you are on the right track with the second example. The part that you are missing is checking the feature scope. SPFarm.Local.FeatureDefinitions is bringing back a collection of all of the features defined in the farm (a collection of SPFeatureDefinition objects). From there, you can check the Scope property of the SPFeatureDefinition object to narrow it down to just the Farm scoped features.

Example:

foreach (SPFeatureDefinition feature in SPFarm.Local.FeatureDefinitions)
{
    if (feature.Scope = "Farm")
    {
        string featureName = feature.DisplayName;
        if (featureName != null)
        {
            XElement newItem = new XElement("Item", featureName);
            infoTree.Add(newItem);
        }
    }

Additional MSDN reference here for the available properties of the SPFeatureDefinition object.

like image 167
Rob Avatar answered Nov 12 '22 13:11

Rob