/// <summary> /// Actually activate/deactivate users specified features on requested site collection list /// </summary> protected internal override bool Execute() { if (command == SiteCollectionCommand.Activate) { log.Info(CommonUIStrings.logFeatureActivate); } else { log.Info(CommonUIStrings.logFeatureDeactivate); } ReadOnlyCollection <Guid?> featureIds = InstallConfiguration.FeatureIdList; if (featureIds == null || featureIds.Count == 0) { log.Warn(CommonUIStrings.logNoFeaturesSpecified); return(true); } if (siteCollectionLocs == null || siteCollectionLocs.Count == 0) { // caller responsible for giving message, depending on operation (install, upgrade,...) return(true); } try { foreach (SiteLoc siteLoc in siteCollectionLocs) { SPSite siteCollection = null; try { siteCollection = new SPSite(siteLoc.SiteId); foreach (Guid?featureId in siteLoc.featureList) { if (featureId == null) { continue; } if (command == SiteCollectionCommand.Activate) { FeatureActivator.ActivateOneFeature(siteCollection.Features, featureId.Value, log); if (!FeatureActivator.IsFeatureActivatedOnSite(siteCollection, featureId.Value)) { // do not add to completedLocs, b/c it didn't complete log.Warn("Activation failed on " + siteCollection.Url + " : " + GetFeatureName(featureId)); } else { completedLocs.Add(siteLoc); log.Info(siteCollection.Url + " : " + GetFeatureName(featureId)); } } else { siteCollection.Features.Remove(featureId.Value, true); completedLocs.Add(siteLoc); log.Info(siteCollection.Url + " : " + GetFeatureName(featureId)); } } } catch (Exception exc) { if (rollback) { // during rollback simply log errors and continue string message; if (command == SiteCollectionCommand.Activate) { message = "Activating feature(s)"; } else { message = "Deactivating feature(s)"; } log.Error(message, exc); } else { log.Error(siteCollection.Url); throw exc; } } finally { // guarantee SPSite is released ASAP even in face of exception if (siteCollection != null) { siteCollection.Dispose(); } } } return(true); } catch (Exception exc) { if (rollback) { log.Error("Error during rollback", exc); return(false); } rollback = true; // rollback work accomplished so far if (command == SiteCollectionCommand.Activate) { DeactivateSiteCollectionFeatureCommand reverseCommand = new DeactivateSiteCollectionFeatureCommand(this.Parent, completedLocs); reverseCommand.Execute(); } else { ActivateSiteCollectionFeatureCommand reverseCommand = new ActivateSiteCollectionFeatureCommand(this.Parent, completedLocs); reverseCommand.Execute(); } throw exc; } }
/// <summary> /// Actually activate/deactivate users specified features on requested site collection list /// </summary> protected internal override bool Execute() { if (command == SiteCollectionCommand.Activate) { log.Info(CommonUIStrings.logFeatureActivate); } else { log.Info(CommonUIStrings.logFeatureDeactivate); } ReadOnlyCollection<Guid?> featureIds = InstallConfiguration.FeatureIdList; if (featureIds == null || featureIds.Count == 0) { log.Warn(CommonUIStrings.logNoFeaturesSpecified); return true; } if (siteCollectionLocs == null || siteCollectionLocs.Count == 0) { // caller responsible for giving message, depending on operation (install, upgrade,...) return true; } try { foreach (SiteLoc siteLoc in siteCollectionLocs) { SPSite siteCollection = null; try { siteCollection = new SPSite(siteLoc.SiteId); foreach (Guid? featureId in siteLoc.featureList) { if (featureId == null) continue; if (command == SiteCollectionCommand.Activate) { FeatureActivator.ActivateOneFeature(siteCollection.Features, featureId.Value, log); if (!FeatureActivator.IsFeatureActivatedOnSite(siteCollection, featureId.Value)) { // do not add to completedLocs, b/c it didn't complete log.Warn("Activation failed on " + siteCollection.Url + " : " + GetFeatureName(featureId)); } else { completedLocs.Add(siteLoc); log.Info(siteCollection.Url + " : " + GetFeatureName(featureId)); } } else { siteCollection.Features.Remove(featureId.Value, true); completedLocs.Add(siteLoc); log.Info(siteCollection.Url + " : " + GetFeatureName(featureId)); } } } catch (Exception exc) { if (rollback) { // during rollback simply log errors and continue string message; if (command == SiteCollectionCommand.Activate) message = "Activating feature(s)"; else message = "Deactivating feature(s)"; log.Error(message, exc); } else { log.Error(siteCollection.Url); throw exc; } } finally { // guarantee SPSite is released ASAP even in face of exception if (siteCollection != null) siteCollection.Dispose(); } } return true; } catch (Exception exc) { if (rollback) { log.Error("Error during rollback", exc); return false; } rollback = true; // rollback work accomplished so far if (command == SiteCollectionCommand.Activate) { DeactivateSiteCollectionFeatureCommand reverseCommand = new DeactivateSiteCollectionFeatureCommand(this.Parent, completedLocs); reverseCommand.Execute(); } else { ActivateSiteCollectionFeatureCommand reverseCommand = new ActivateSiteCollectionFeatureCommand(this.Parent, completedLocs); reverseCommand.Execute(); } throw exc; } }