SharePoint 2007 : vérifier si une feature est activée

April 20, 2010

Dans un développement SharePoint, j’ai eu besoin de savoir si une fonctionnalité était activée ou non.

Voici un petit bout de code qui permet de savoir si une fonctionnalité est activée ou non, selon son scope :

/// <summary>
/// Check if a site scoped feature is activated
/// </summary>
/// <param name="site">SPSite to test</param>
/// <param name="featureId">Id of the feature</param>
/// <returns><c>true</c> if the feature is activated, otherwise <c>false</c>.</returns>
private bool CheckIfSiteFeatureActivated(SPSite site, Guid featureId)
{
  foreach (SPFeature feature in site.Features)
  {
    if (feature.DefinitionId == featureId) return true;
  }
  return false;
}
/// <summary>
/// Check if a web scoped feature is activated
/// </summary>
/// <param name="web">SPWeb to test</param>
/// <param name="featureId">Id of the feature</param>
/// <returns><c>true</c> if the feature is activated, otherwise <c>false</c>.</returns>
private bool CheckIfWebFeatureActivated(SPWeb web, Guid featureId)
{
  foreach (SPFeature feature in web.Features)
  {
    if (feature.DefinitionId == featureId) return true;
  }
  return false;
}
/// <summary>
/// Check if a web applicaiton scoped feature is activated
/// </summary>
/// <param name="webApp">SPWebApplication to test</param>
/// <param name="featureId">Id of the feature</param>
/// <returns><c>true</c> if the feature is activated, otherwise <c>false</c>.</returns>
private bool CheckIfWebFeatureActivated(SPWebApplication webApp, Guid featureId)
{
  foreach (SPFeature feature in webApp.Features)
  {
    if (feature.DefinitionId == featureId) return true;
  }
  return false;
}

Ici le code est en Framework 3.0, mais vous pouvez bien entendu ajouter le mot clé this pour créer des méthodes d’extension pour les versions 3.5 et supérieures du framework :

/// <summary>
/// Check if a site scoped feature is activated
/// </summary>
/// <param name="site">SPSite to test</param>
/// <param name="featureId">Id of the feature</param>
/// <returns><c>true</c> if the feature is activated, otherwise <c>false</c>.</returns>
private bool CheckIfSiteFeatureActivated(this SPSite site, Guid featureId)
{
  foreach (SPFeature feature in site.Features)
  {
    if (feature.DefinitionId == featureId) return true;
  }
  return false;
}
/// <summary>
/// Check if a web scoped feature is activated
/// </summary>
/// <param name="web">SPWeb to test</param>
/// <param name="featureId">Id of the feature</param>
/// <returns><c>true</c> if the feature is activated, otherwise <c>false</c>.</returns>
private bool CheckIfWebFeatureActivated(this SPWeb web, Guid featureId)
{
  foreach (SPFeature feature in web.Features)
  {
    if (feature.DefinitionId == featureId) return true;
  }
  return false;
}
/// <summary>
/// Check if a web applicaiton scoped feature is activated
/// </summary>
/// <param name="webApp">SPWebApplication to test</param>
/// <param name="featureId">Id of the feature</param>
/// <returns><c>true</c> if the feature is activated, otherwise <c>false</c>.</returns>
private bool CheckIfWebFeatureActivated(this SPWebApplication webApp, Guid featureId)
{
  foreach (SPFeature feature in webApp.Features)
  {
    if (feature.DefinitionId == featureId) return true;
  }
  return false;
}