I’m back again with another SharePoint question!
I have an application that returns a list of features on the local SharePoint farm. But the issue is that it seems to return them as ‘Online’ whether or not they are activated.
For example, this feature would still show up in my app as ‘Online’ even though it is not activated:
The code I am using currently is as follows:
foreach (SPFeatureDefinition featureDefinition in featureCollection)
{
if (featureDefinition.Scope.Equals(scopeSelect))
{
string featureName = featureDefinition.DisplayName;
XElement newItem = new XElement("Item", featureName);
XElement itemStatus = new XElement("Item", featureDefinition.Status);
infoTree.Add(newItem);
infoTree.Add(itemStatus);
}
}
}
The issue is that featureDefinition.Status always returns ‘Online’ regardless of the actual status of the feature. This leads me to believe I am doing something wrong, or the .Status property isn’t actually what I am looking for.
Is there another property I can use that would set me on the right track?
Any guidance/suggestions will be most appreciated!
EDIT:
Just as an aside, an IIS reset does not have any effect on it returning a different status.
Yes, the
.Statusproperty will tell you the status of theDefinition, but no the activation status of the feature at a specific scope. You have a to understand a bit about features for this: A feature definition is just the template – you activate an instance of the feature at specific level (farm, webapp, site, web) – hence you can’t check the status of the definition, but need to check whether the instance of the feature exists at the specific scope.To check the feature status via PowerShell you can just use
Get-SPFeatureas described here. As you can see, all the code is doing is calling Get-SPFeature and if it returns something for a specific scope the feature is activated. You can do the same thing in C#.So when you want to check whether a feature is activated e.g. on site scope you can just iterate over
SPSite.Features, when you find the feature you know it is activated. The Powershell commandlet isn’t doing anything else.You want to check a farm feature, so you can check the collection at
SPWebService.ContentService.Featuresto check for activated farm features. See another example of a PowerShell script (almost looks like C#) here.