private static int ToggleFeatureInFeatureCollection(SPFeatureCollection features, Guid featureId, bool activate, bool force) { var featuresModifiedCounter = 0; if (activate) { // activate feature var feature = features.Add(featureId, force); if (feature != null) { featuresModifiedCounter++; } } else { // deactivate feature var featuresActiveBefore = features.Count(); features.Remove(featureId, force); if (featuresActiveBefore > features.Count) { featuresModifiedCounter++; } } return(featuresModifiedCounter); }
private static int ProcessSingleFeatureInFeatureCollection(Guid parentId, SPFeatureCollection features, Guid featureId, bool activate, bool force, out Exception exception) { exception = null; var featuresModifiedCounter = 0; if (features == null) { exception = new ArgumentNullException("feature collection was null"); return(featuresModifiedCounter); } try { if (activate) { // activate feature var feature = features.Add(featureId, force); if (feature != null) { featuresModifiedCounter++; SingletonDb.Singleton.InMemoryDb.AddActivatedFeature(feature); } } else { // deactivate feature var featuresActiveBefore = features.Count(); features.Remove(featureId, force); if (featuresActiveBefore > features.Count) { featuresModifiedCounter++; SingletonDb.Singleton.InMemoryDb.RemoveDeactivatedFeature(featureId, parentId); } } } catch (Exception ex) { exception = ex; } return(featuresModifiedCounter); }
/// <summary> /// deactivate a feature /// </summary> /// <param name="features">collection of features</param> /// <param name="featureId">feature ID of feature to handle</param> /// <param name="force">with or without force</param> /// <remarks>attention, might throw exception!</remarks> private static void DeactivateFeatureInFeatureCollection(SPFeatureCollection features, Guid featureId, bool force) { var featuresActiveBefore = features.Count(); features.Remove(featureId, force); if (!(featuresActiveBefore > features.Count)) { var errMsg = string.Format("Feature '{0}' was not removed.", featureId); if (!force) { errMsg += " You might want to try again with 'force' enabled."; } throw new ApplicationException(errMsg); } return; }