【问题标题】:Get Farm Features from SharePoint 2010从 SharePoint 2010 获取场功能
【发布时间】:2012-10-11 15:19:15
【问题描述】:

我正在尝试从 SharePoint 2010 管理中心网站获取场功能列表。我遇到的问题是我只成功撤回了 Site 功能。以下代码是我目前正在使用的代码:

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

}

我也试过使用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);
    }

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

编辑 我一直在玩弄

SPFeatureCollection featureCollect = SPContext.Current.Site.Features  

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

【问题讨论】:

标签: c# sharepoint sharepoint-2010


【解决方案1】:

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

例子:

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);
        }
    }

关于 SPFeatureDefinition 对象的可用属性的其他 MSDN 参考 here

【讨论】:

  • 啊,是的,我完全忘了查看 Scope 设置。感谢您的建议/示例。我会开始玩这个!
  • 范围确实是答案!它不喜欢features.Scope = "Farm",而是在 if 之前声明一个范围,然后比较:features.Scope.Equals(scopeSelect) 成功了。感谢您的帮助。
  • 对我的语法错误感到抱歉。我在仔细检查时使用了 PowerShell 脚本。该脚本如下所示: $farm = get-spfarm; foreach ($feature in $farm.FeatureDefinitions) { if ($feature.Scope -eq "Farm") { write-host $feature.name $feature.FeatureDefinitionScope } } 很高兴这对你有用!
  • 没问题!它让我不再只是复制/粘贴,我学到了更多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2011-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多