private void InnerEnsureFeatureActivation(FeatureDependencyInfo featureDependency, SPFeatureCollection featureCollection) { // If already activated if (featureCollection.Any(sf => sf.DefinitionId == featureDependency.FeatureId)) { if (featureDependency.ForceReactivation) { this.logger.Info( "Disactivating the feature with id '{0}' because the 'ForceReactivation' property was used.", featureDependency.FeatureId); // Deactivate and reactivate feature featureCollection.Remove(featureDependency.FeatureId); featureCollection.Add(featureDependency.FeatureId); } else { this.logger.Warn( @"Feature with id '{0}' is already activated. If you wish to force it's reactivation, please use the 'ForceReactivation' property.", featureDependency.FeatureId); } } else { // Activate feature featureCollection.Add(featureDependency.FeatureId); } }
public static void ActivateWebFeature(string webUrl, string featureId, bool force = false) { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(webUrl)) using (SPWeb newWeb = site.OpenWeb()) { Guid id = new Guid(featureId); SPFeatureCollection featureCollection = newWeb.Features; if (force || featureCollection[id] == null) { if (featureCollection[id] != null) { try { newWeb.AllowUnsafeUpdates = true; featureCollection.Remove(id, force); newWeb.Update(); } catch { } } newWeb.AllowUnsafeUpdates = true; featureCollection.Add(id, force); newWeb.Update(); newWeb.AllowUnsafeUpdates = false; } } }); }
public static void DeactivateFeatureInWeb(string strSiteUrl, string strFeatureTitle) { using (SPSite site = new SPSite(strSiteUrl)) { System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(1033); SPFeatureDefinitionCollection featureDefinitionColl = SPFarm.Local.FeatureDefinitions; SPWebCollection webColl = site.AllWebs; foreach (SPFeatureDefinition featureDefinition in featureDefinitionColl) { if (featureDefinition.GetTitle(cultureInfo) == strFeatureTitle) { Guid guidFeatureDefinitionID = featureDefinition.Id; foreach (SPWeb web in webColl) { if (featureDefinition.Scope == SPFeatureScope.Web) { SPFeatureCollection featureColl = web.Features; featureColl.Remove(guidFeatureDefinitionID, true); } web.Dispose(); } } } } }
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); }
public static void EnsureDeactivated(this SPFeatureCollection features, Guid featureId) { if (IsActivated(features, featureId)) { features.Remove(featureId); } }
public void Remove(object featureId, object force) { if (featureId == Undefined.Value) { throw new JavaScriptException(this.Engine, "Error", "A Feature Id must be provided as the first argument."); } var featureGuid = GuidInstance.ConvertFromJsObjectToGuid(featureId); if (force == null || force == Null.Value || force == Undefined.Value) { m_featureCollection.Remove(featureGuid); return; } m_featureCollection.Remove(featureGuid, TypeConverter.ToBoolean(force)); }
/// <summary>forcefully removes a feature from a featurecollection</summary> /// <param name="id">Feature ID</param> public void ForceRemoveFeature(Guid id) { try { _spfeatures.Remove(id, true); } catch { } }
/// <summary> /// Activates or deactivates the feature. /// </summary> /// <param name="features">The features.</param> /// <param name="activate">if set to <c>true</c> [activate].</param> /// <param name="featureId">The feature id.</param> /// <param name="urlScope">The URL scope.</param> /// <param name="force">if set to <c>true</c> [force].</param> /// <param name="ignoreNonActive">if set to <c>true</c> [ignore non active].</param> /// <returns></returns> private SPFeature ActivateDeactivateFeature(SPFeatureCollection features, bool activate, Guid featureId, string urlScope, bool force, bool ignoreNonActive) { if (features[featureId] == null && ignoreNonActive) { return(null); } if (!activate) { if (features[featureId] != null || force) { Logger.Write("Progress: Deactivating Feature {0} from {1}.", featureId.ToString(), urlScope); try { features.Remove(featureId, force); } catch (Exception ex) { Logger.WriteWarning("{0}", ex.Message); } } else { Logger.WriteWarning("" + SPResource.GetString("FeatureNotActivatedAtScope", new object[] { featureId }) + " Use the -force parameter to force a deactivation."); } return(null); } if (features[featureId] == null) { Logger.Write("Progress: Activating Feature {0} on {1}.", featureId.ToString(), urlScope); } else { if (!force) { SPFeatureDefinition fd = features[featureId].Definition; Logger.WriteWarning("" + SPResource.GetString("FeatureAlreadyActivated", new object[] { fd.DisplayName, fd.Id, urlScope }) + " Use the -force parameter to force a reactivation."); return(features[featureId]); } Logger.Write("Progress: Re-Activating Feature {0} on {1}.", featureId.ToString(), urlScope); } try { return(features.Add(featureId, force)); } catch (Exception ex) { Logger.WriteException(new System.Management.Automation.ErrorRecord(ex, null, System.Management.Automation.ErrorCategory.NotSpecified, features)); return(null); } }
/// <summary>forcefully removes a feature from a featurecollection</summary> /// <param name="id">Feature ID</param> public void ForceRemoveFeatureFromLocation(Location location, SPFeatureCollection spfeatureSet, Guid featureId) { try { spfeatureSet.Remove(featureId, true); } catch (Exception exc) { logException(exc, string.Format( "Trying to remove feature {0} from {1}", featureId, LocationManager.SafeDescribeLocation(location))); } }
private void DisableFeatures(string site) { try { using (SPWeb webNew = new SPSite(site).OpenWeb()) { bool isRootWeb = webNew.IsRootWeb; if (isRootWeb) { foreach (feature sfeature in disableSiteFeatures) { try { using (SPWeb web = new SPSite(site).OpenWeb()) { SPFeatureCollection siteFeaturesCollection = web.Site.Features; //var feature = siteFeaturesCollection.SingleOrDefault(f => f.DefinitionId == sfeature.definitionId); output.Append(string.Format("de-activating site feature: {0}" + Environment.NewLine, sfeature.name)); siteFeaturesCollection.Remove(sfeature.definitionId, true); } } catch { output.Append(string.Format("error de-activating site feature: {0}" + Environment.NewLine, sfeature.name)); } } } foreach (feature wfeature in disableWebFeatures) { try { using (SPWeb web = new SPSite(site).OpenWeb()) { SPFeatureCollection webFeaturesCollection = web.Features; //var feature = webFeaturesCollection.SingleOrDefault(f => f.DefinitionId == wfeature.definitionId); output.Append(string.Format("de-activating web feature: {0}" + Environment.NewLine, wfeature.name)); webFeaturesCollection.Remove(wfeature.definitionId, true); } } catch { output.Append(string.Format("error de-activating web feature: {0}" + Environment.NewLine, wfeature.name)); } } } } catch { } }
private void PerformAction(object locobj, SPFeatureCollection spFeatureCollection, Feature feature) { try { if (_action == Action.Activating) { if (!(spFeatureCollection[feature.Id] is SPFeature)) { ++ActivationAttempts; spFeatureCollection.Add(feature.Id); if ((spFeatureCollection[feature.Id] is SPFeature)) { _featureDb.RecordFeatureActivation(locobj, feature.Id); ++Activations; } } } else { if ((spFeatureCollection[feature.Id] is SPFeature)) { ++ActivationAttempts; spFeatureCollection.Remove(feature.Id); if (!(spFeatureCollection[feature.Id] is SPFeature)) { _featureDb.RecordFeatureDeactivation(locobj, feature.Id); ++Activations; } } } } catch (Exception exc) { LogException(exc, string.Format( "{0} feature {1} on {2}: {3}", _action, feature.Id, feature.Scope, LocationManager.SafeDescribeObject(locobj) )); } }
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; }
//public override void FeatureInstalled(SPFeatureReceiverProperties properties) //{ //} //public override void FeatureUninstalling(SPFeatureReceiverProperties properties) //{ //} //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters) //{ //} #region - Uninstalling Feature Methods - //private void UninstallingFeatureMethod() //{ // if (!EventLog.SourceExists("TESTzapisu")) // EventLog.CreateEventSource("TESTzapisu", "Application"); // EventLog.WriteEntry("TESTzapisu", "Uninstalling feature", EventLogEntryType.Error); // try // { //SPWeb currentWeb = (properties.Feature.Parent as SPSite).OpenWeb(); //SPSite currentSite1 = properties.Feature.Parent as SPSite; //SPWebApplication currentWebApp = currentWeb.Site.WebApplication; //if (!EventLog.SourceExists("TESTzapisu")) // EventLog.CreateEventSource("TESTzapisu", "Application"); //EventLog.WriteEntry("TESTzapisu", "WebApplication - DisplayName:" + currentWebApp.DisplayName + " Name:" + currentWebApp.Name, EventLogEntryType.Error); //EventLog.WriteEntry("TESTzapisu", "currentSite - HostName:" + currentSite1.HostName + " Url:" + currentSite1.Url, EventLogEntryType.Error); //SPSiteCollection currentSiteColl = currentWebApp.Sites; //foreach (SPSite currentSite in currentSiteColl) //{ // EventLog.WriteEntry("TESTzapisu", "Site - Url:" + currentSite.Url + " HostName:" + currentSite.HostName + " PortalName:" + currentSite.PortalName + " RootWeb.Name:" + currentSite.RootWeb.Name, EventLogEntryType.Error); // currentSite.Features.Remove(Definitions.featureGuid, true); //} //} //catch (Exception ex) //{ // if (!EventLog.SourceExists("TESTzapisu")) // EventLog.CreateEventSource("TESTzapisu", "Application"); // EventLog.WriteEntry("TESTzapisu", "Chyba pri odinstalovani", EventLogEntryType.Error); //} //} #endregion #region - Info how to get current SPWeb or SPSite if SPContext is null - //Ak je featura typu scope SITE //SPWeb objSPWeb = (properties.Feature.Parent as SPSite).OpenWeb(); //SPWeb objSPWeb = (properties.Feature.Parent as SPSite).RootWeb; ///Ak je featura typu scope WEB //SPWeb objSPWeb = properties.Feature.Parent as SPWeb; //SPWeb objSPWeb = properties.Feature.Parent as SPWeb; #endregion #region - Custom Methods - //http://msdn.microsoft.com/en-us/library/ee231545.aspx //http://ranaictiu-technicalblog.blogspot.com/2010/10/sharepoint-activedeactivate-feature.html#%7B%22color%22%3A%22%23000000%22%2C%22backgroundColor%22%3A%22%23f6f6f6%22%2C%22unvisitedLinkColor%22%3A%22%23de7008%22%2C%22fontFamily%22%3A%22%5C%22Trebuchet%20MS%5C%22%2C%20Trebuchet%2C%20Verdana%2C%20Sans-Serif%22%7D //http://www.sharepointer.com.br/sharepoint-development/sharepoint-object-model/how-to-activate-a-feature-in-a-sharepoint-farm-programmatically/s public static void DeactivateFeatureInFarm(string strFeatureTitle) { SPFarm farm = SPFarm.Local; SPWebService webService = farm.Services.GetValue <SPWebService>(""); System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo(1033); SPFeatureDefinitionCollection featureDefinitionColl = SPFarm.Local.FeatureDefinitions; foreach (SPFeatureDefinition featureDefinition in featureDefinitionColl) { if (featureDefinition.GetTitle(cultureInfo) == strFeatureTitle) { Guid guidFeatureDefinitionID = featureDefinition.Id; if (featureDefinition.Scope == SPFeatureScope.Farm) { SPFeatureCollection featureColl = webService.Features; featureColl.Remove(guidFeatureDefinitionID, true); } } } }
private void ProcessFeature( object modelHost, SPFeatureCollection features, FeatureDefinition featureModel) { var currentFeature = GetFeature(features, featureModel); var featureActivated = IsFeatureActivated(currentFeature); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFeature, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Is feature activated: [{0}]", featureActivated); if (!featureActivated) { if (featureModel.Enable) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Enabling feature"); var f = SafelyActivateFeature(features, featureModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = f, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFeature, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } } else { if (featureModel.Enable && featureModel.ForceActivate) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Feature enabled, but ForceActivate = true. Force activating."); var f = SafelyActivateFeature(features, featureModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = f, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else if (!featureModel.Enable) { TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Removing feature."); features.Remove(featureModel.Id, featureModel.ForceActivate); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = null, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFeature, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } } }
/// <summary> /// Activates or deactivates the feature. /// </summary> /// <param name="features">The features.</param> /// <param name="activate">if set to <c>true</c> [activate].</param> /// <param name="featureId">The feature id.</param> /// <param name="urlScope">The URL scope.</param> /// <param name="force">if set to <c>true</c> [force].</param> /// <param name="ignoreNonActive">if set to <c>true</c> [ignore non active].</param> /// <returns></returns> private SPFeature ActivateDeactivateFeature(SPFeatureCollection features, bool activate, Guid featureId, string urlScope, bool force, bool ignoreNonActive) { if (features[featureId] == null && ignoreNonActive) return null; if (!activate) { if (features[featureId] != null || force) { Logger.Write("Progress: Deactivating Feature {0} from {1}.", featureId.ToString(), urlScope); try { features.Remove(featureId, force); } catch (Exception ex) { Logger.WriteWarning("{0}", ex.Message); } } else { Logger.WriteWarning("" + SPResource.GetString("FeatureNotActivatedAtScope", new object[] { featureId }) + " Use the -force parameter to force a deactivation."); } return null; } if (features[featureId] == null) Logger.Write("Progress: Activating Feature {0} on {1}.", featureId.ToString(), urlScope); else { if (!force) { SPFeatureDefinition fd = features[featureId].Definition; Logger.WriteWarning("" + SPResource.GetString("FeatureAlreadyActivated", new object[] { fd.DisplayName, fd.Id, urlScope }) + " Use the -force parameter to force a reactivation."); return features[featureId]; } Logger.Write("Progress: Re-Activating Feature {0} on {1}.", featureId.ToString(), urlScope); } try { return features.Add(featureId, force); } catch(Exception ex) { Logger.WriteException(new System.Management.Automation.ErrorRecord(ex, null, System.Management.Automation.ErrorCategory.NotSpecified, features)); return null; } }
private void ProcessFeature( object modelHost, SPFeatureCollection features, FeatureDefinition featureModel) { var currentFeature = features.FirstOrDefault(f => f.DefinitionId == featureModel.Id); var featureActivated = currentFeature != null; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = currentFeature, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); if (!featureActivated) { if (featureModel.Enable) { var f = features.Add(featureModel.Id, featureModel.ForceActivate); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = f, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFeature, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } } else { if (featureModel.Enable && featureModel.ForceActivate) { var f = features.Add(featureModel.Id, featureModel.ForceActivate); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = f, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else if (!featureModel.Enable) { features.Remove(featureModel.Id, featureModel.ForceActivate); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = null, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } else { InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = currentFeature, ObjectType = typeof(SPFeature), ObjectDefinition = featureModel, ModelHost = modelHost }); } } }
public static void DisableFeature(ISharePointCommandContext context, FeatureInfo featureID) { SPFeatureCollection featureCollection = GetFeatureCollectionForFeature(context, featureID); featureCollection.Remove(featureID.FeatureID, true); }
private void PerformAction(Location location, SPFeatureCollection spFeatureCollection, Feature feature) { try { if (_action == Action.Activating) { if (!(spFeatureCollection[feature.Id] is SPFeature)) { ++ActivationAttempts; try { spFeatureCollection.Add(feature.Id); if ((spFeatureCollection[feature.Id] is SPFeature)) { _featureDb.RecordFeatureActivationAtLocation(location, feature.Id); ++Activations; string msg = string.Format("Activated feature {0} ({1}:{2}) from location", feature.Id, feature.Scope, feature.Name); LogInfo(location, msg); } else { string msg = string.Format("Failure activating feature {0} ({1}:{2}) from location", feature.Id, feature.Scope, feature.Name); LogInfo(location, msg); } } catch (Exception exc) { string msg = string.Format("Exception activating feature {0} ({1}:{2}) from location", feature.Id, feature.Scope, feature.Name); LogException(exc, location, msg); } } } else { if ((spFeatureCollection[feature.Id] is SPFeature)) { ++ActivationAttempts; try { spFeatureCollection.Remove(feature.Id); if (!(spFeatureCollection[feature.Id] is SPFeature)) { _featureDb.RecordFeatureDeactivationAtLocation(location, feature.Id); ++Activations; string msg = string.Format("Removed feature {0} ({1}:{2}) from location", feature.Id, feature.Scope, feature.Name); LogInfo(location, msg); } else { string msg = string.Format("Failure removing feature {0} ({1}:{2}) from location", feature.Id, feature.Scope, feature.Name); LogInfo(location, msg); } } catch (Exception exc) { string msg = string.Format("Exception removing feature {0} ({1}:{2}) from location", feature.Id, feature.Scope, feature.Name); LogException(exc, location, msg); } } } } catch (Exception exc) { string msg = string.Format("Exception {0} feature {1} ({2}:{3}) at location", _action, feature.Id, feature.Scope, feature.Name); LogException(exc, location, msg); } }