从SharePoint 2010获取场功能

wjh*_*man 5 c# sharepoint sharepoint-2010

我试图从SharePoint 2010 Central Admin站点获取Farm功能列表.我遇到的问题是我只是成功撤回了网站功能.以下代码是我目前正在使用的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用SPFarm.Local.FeatureDefinitions 如下:

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

但无济于事.我正在接近的下一个途径是使用SPFeatureCollection.我可以采取更好的方法解决这个问题吗?基本上我只是在寻找一些线索,因为我还没有得到任何东西SPFeatureCollection.

编辑 我一直在搞乱

SPFeatureCollection featureCollect = SPContext.Current.Site.Features  
Run Code Online (Sandbox Code Playgroud)

但到目前为止,我遇到了SPContext返回null 的问题.

Rob*_*Rob 2

我认为第二个例子你是在正确的轨道上。您缺少的部分是检查功能范围。SPFarm.Local.FeatureDefinitions 返回场中定义的所有功能的集合(SPFeatureDefinition 对象的集合)。从那里,您可以检查 SPFeatureDefinition 对象的 Scope 属性,将其范围缩小到仅 Farm 范围内的功能。

例子:

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

有关 SPFeatureDefinition 对象的可用属性的其他 MSDN 参考请参见此处。