SharePoint Feature Activation Extension Methods

Extend your SharePoint Features collection with an IsActivated, EnsureActivated, and EnsureDeactivated methods. These extensions will be available for activating or deactivating features for a Web, Site Collection, Web Application, and Farm.

public static class SPFeatureCollectionExtensions
{
  public static bool IsActivated(this SPFeatureCollection features, Guid featureId)
   {
    return features.Cast<SPFeature>().FirstOrDefault(f => f.DefinitionId.Equals(featureId)) != null;
   }
  public static bool IsActivated(this SPFeatureCollection features, string featureId)
   {
    return IsActivated(features, new Guid(featureId));
   }
  public static void EnsureActivated(this SPFeatureCollection features, Guid featureId)
  {
    if (!IsActivated(features, featureId))
      features.Add(featureId);
  }
  public static void EnsureActivated(this SPFeatureCollection features, string featureId)
  {
    EnsureActivated(features, new Guid(featureId));
  }
  public static void EnsureDeactivated(this SPFeatureCollection features, Guid featureId)
  {
    if (IsActivated(features, featureId))
      features.Remove(featureId);
}
public static void EnsureDeactivated(this SPFeatureCollection features, string featureId)
  {
    EnsureDeactivated(features, new Guid(featureId));
  }
}

Usage Examples:

web.Features.IsActivated("999974A3-E65F-4426-AC75-FAD64EF49999");
site.Features.EnsureActivated("999974A3-E65F-4426-AC75-FAD64EF49999");
webApp.Features.EnsureActivated("999974A3-E65F-4426-AC75-FAD64EF49999");
farm.Features.EnsureActivated("999974A3-E65F-4426-AC75-FAD64EF49999");
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s