protected override void UpdateDataObject() { SPFeatureDefinition featureDef = null; ActivationScope scope = ActivationScope.Feature; if (DeactivateAtScope.HasValue) scope = DeactivateAtScope.Value; if (farmLevelFeature) { featureDef = SPFarm.Local.FeatureDefinitions[base.DataObject.Id]; } else { featureDef = base.DataObject; } try { Logger.Write("Started at {0}", DateTime.Now.ToString()); Guid featureId = featureDef.Id; FeatureHelper fh = new FeatureHelper(); fh.ActivateDeactivateFeatureAtScope(featureDef, scope, false, Url, Force.IsPresent, IgnoreNonActive.IsPresent); } finally { Logger.Write("Finished at {0}\r\n", DateTime.Now.ToString()); } if ((featureDef != null) && (PassThru.IsPresent)) { base.DataObject = featureDef; base.WriteResult(base.DataObject); } }
/// <summary> /// Activates or deactivates the feature at the specified scope. /// </summary> /// <param name="scope">The scope.</param> /// <param name="featureId">The feature id.</param> /// <param name="activate">if set to <c>true</c> [activate].</param> /// <param name="url">The URL.</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> internal void ActivateDeactivateFeatureAtScope(ActivationScope scope, Guid featureId, bool activate, string url, bool force, bool ignoreNonActive) { Logger.Verbose = true; m_FeatureId = featureId; if (m_FeatureId.Equals(Guid.Empty)) { throw new SPException("Unable to locate Feature."); } SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions[m_FeatureId]; if (feature == null) { throw new SPException("Unable to locate Feature."); } if (scope == ActivationScope.Feature) { scope = (ActivationScope)Enum.Parse(typeof(ActivationScope), feature.Scope.ToString().ToLowerInvariant(), true); } ActivateDeactivateFeatureAtScope(feature, scope, activate, url, force, ignoreNonActive); }
/// <summary> /// Executes the specified command. /// </summary> /// <param name="command">The command.</param> /// <param name="keyValues">The key values.</param> /// <param name="output">The output.</param> /// <returns></returns> public override int Execute(string command, StringDictionary keyValues, out string output) { output = string.Empty; Logger.Verbose = true; ActivationScope scope = ActivationScope.Feature; if (Params["scope"].UserTypedIn) { scope = (ActivationScope)Enum.Parse(typeof(ActivationScope), Params["scope"].Value.ToLowerInvariant(), true); } bool force = Params["force"].UserTypedIn; bool ignoreNonActive = Params["ignorenonactive"].UserTypedIn; if (ignoreNonActive) { force = true; } string url = null; if (Params["url"].UserTypedIn) { url = Params["url"].Value.TrimEnd('/'); } try { Logger.Write("Started at {0}", DateTime.Now.ToString()); Guid featureId = FeatureHelper.GetFeatureIdFromParams(Params); FeatureHelper fh = new FeatureHelper(); fh.ActivateDeactivateFeatureAtScope(scope, featureId, false, url, force, ignoreNonActive); } finally { Logger.Write("Finished at {0}\r\n", DateTime.Now.ToString()); } return((int)ErrorCodes.NoError); }
internal void ActivateDeactivateFeatureAtScope(SPFeatureDefinition feature, ActivationScope scope, bool activate, string url, bool force, bool ignoreNonActive) { m_IgnoreNonActive = ignoreNonActive; m_Activate = activate; m_Force = force; m_Url = url; m_FeatureId = feature.Id; if (feature.Scope == SPFeatureScope.Farm) { if (scope != ActivationScope.Farm) { throw new SPSyntaxException("The Feature specified is scoped to the Farm. The -scope parameter must be \"Farm\"."); } ActivateDeactivateFeatureAtFarm(activate, m_FeatureId, m_Force, m_IgnoreNonActive); } else if (feature.Scope == SPFeatureScope.WebApplication) { if (scope != ActivationScope.Farm && scope != ActivationScope.WebApplication) { throw new SPSyntaxException("The Feature specified is scoped to the Web Application. The -scope parameter must be either \"Farm\" or \"WebApplication\"."); } if (scope == ActivationScope.Farm) { SPEnumerator enumerator = new SPEnumerator(SPFarm.Local); enumerator.SPWebApplicationEnumerated += enumerator_SPWebApplicationEnumerated; enumerator.Enumerate(); } else { if (string.IsNullOrEmpty(m_Url)) { throw new SPSyntaxException("The -url parameter is required if the scope is \"WebApplication\"."); } SPWebApplication webApp = SPWebApplication.Lookup(new Uri(m_Url)); ActivateDeactivateFeatureAtWebApplication(webApp, m_FeatureId, activate, m_Force, m_IgnoreNonActive); } } else if (feature.Scope == SPFeatureScope.Site) { if (scope == ActivationScope.Web) { throw new SPSyntaxException("The Feature specified is scoped to Site. The -scope parameter cannot be \"Web\"."); } SPSite site = null; SPEnumerator enumerator = null; try { if (scope == ActivationScope.Farm) { enumerator = new SPEnumerator(SPFarm.Local); } else if (scope == ActivationScope.WebApplication) { SPWebApplication webApp = SPWebApplication.Lookup(new Uri(m_Url)); enumerator = new SPEnumerator(webApp); } else if (scope == ActivationScope.Site) { site = new SPSite(m_Url); ActivateDeactivateFeatureAtSite(site, activate, m_FeatureId, m_Force, m_IgnoreNonActive); } if (enumerator != null) { enumerator.SPSiteEnumerated += enumerator_SPSiteEnumerated; enumerator.Enumerate(); } } finally { if (site != null) { site.Dispose(); } } } else if (feature.Scope == SPFeatureScope.Web) { SPSite site = null; SPWeb web = null; SPEnumerator enumerator = null; try { if (scope == ActivationScope.Farm) { enumerator = new SPEnumerator(SPFarm.Local); } else if (scope == ActivationScope.WebApplication) { SPWebApplication webApp = SPWebApplication.Lookup(new Uri(m_Url)); enumerator = new SPEnumerator(webApp); } else if (scope == ActivationScope.Site) { site = new SPSite(m_Url); enumerator = new SPEnumerator(site); } else if (scope == ActivationScope.Web) { site = new SPSite(m_Url); web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(m_Url)]; enumerator = new SPEnumerator(web); } if (enumerator != null) { enumerator.SPWebEnumerated += enumerator_SPWebEnumerated; enumerator.Enumerate(); } } finally { if (web != null) { web.Dispose(); } if (site != null) { site.Dispose(); } } } }
internal void ActivateDeactivateFeatureAtScope(SPFeatureDefinition feature, ActivationScope scope, bool activate, string url, bool force, bool ignoreNonActive) { m_IgnoreNonActive = ignoreNonActive; m_Activate = activate; m_Force = force; m_Url = url; m_FeatureId = feature.Id; if (feature.Scope == SPFeatureScope.Farm) { if (scope != ActivationScope.Farm) throw new SPSyntaxException("The Feature specified is scoped to the Farm. The -scope parameter must be \"Farm\"."); ActivateDeactivateFeatureAtFarm(activate, m_FeatureId, m_Force, m_IgnoreNonActive); } else if (feature.Scope == SPFeatureScope.WebApplication) { if (scope != ActivationScope.Farm && scope != ActivationScope.WebApplication) throw new SPSyntaxException("The Feature specified is scoped to the Web Application. The -scope parameter must be either \"Farm\" or \"WebApplication\"."); if (scope == ActivationScope.Farm) { SPEnumerator enumerator = new SPEnumerator(SPFarm.Local); enumerator.SPWebApplicationEnumerated += enumerator_SPWebApplicationEnumerated; enumerator.Enumerate(); } else { if (string.IsNullOrEmpty(m_Url)) throw new SPSyntaxException("The -url parameter is required if the scope is \"WebApplication\"."); SPWebApplication webApp = SPWebApplication.Lookup(new Uri(m_Url)); ActivateDeactivateFeatureAtWebApplication(webApp, m_FeatureId, activate, m_Force, m_IgnoreNonActive); } } else if (feature.Scope == SPFeatureScope.Site) { if (scope == ActivationScope.Web) throw new SPSyntaxException("The Feature specified is scoped to Site. The -scope parameter cannot be \"Web\"."); SPSite site = null; SPEnumerator enumerator = null; try { if (scope == ActivationScope.Farm) enumerator = new SPEnumerator(SPFarm.Local); else if (scope == ActivationScope.WebApplication) { SPWebApplication webApp = SPWebApplication.Lookup(new Uri(m_Url)); enumerator = new SPEnumerator(webApp); } else if (scope == ActivationScope.Site) { site = new SPSite(m_Url); ActivateDeactivateFeatureAtSite(site, activate, m_FeatureId, m_Force, m_IgnoreNonActive); } if (enumerator != null) { enumerator.SPSiteEnumerated += enumerator_SPSiteEnumerated; enumerator.Enumerate(); } } finally { if (site != null) site.Dispose(); } } else if (feature.Scope == SPFeatureScope.Web) { SPSite site = null; SPWeb web = null; SPEnumerator enumerator = null; try { if (scope == ActivationScope.Farm) enumerator = new SPEnumerator(SPFarm.Local); else if (scope == ActivationScope.WebApplication) { SPWebApplication webApp = SPWebApplication.Lookup(new Uri(m_Url)); enumerator = new SPEnumerator(webApp); } else if (scope == ActivationScope.Site) { site = new SPSite(m_Url); enumerator = new SPEnumerator(site); } else if (scope == ActivationScope.Web) { site = new SPSite(m_Url); web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(m_Url)]; enumerator = new SPEnumerator(web); } if (enumerator != null) { enumerator.SPWebEnumerated += enumerator_SPWebEnumerated; enumerator.Enumerate(); } } finally { if (web != null) web.Dispose(); if (site != null) site.Dispose(); } } }
/// <summary> /// Activates or deactivates the feature at the specified scope. /// </summary> /// <param name="scope">The scope.</param> /// <param name="featureId">The feature id.</param> /// <param name="activate">if set to <c>true</c> [activate].</param> /// <param name="url">The URL.</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> internal void ActivateDeactivateFeatureAtScope(ActivationScope scope, Guid featureId, bool activate, string url, bool force, bool ignoreNonActive) { Logger.Verbose = true; m_FeatureId = featureId; if (m_FeatureId.Equals(Guid.Empty)) throw new SPException("Unable to locate Feature."); SPFeatureDefinition feature = SPFarm.Local.FeatureDefinitions[m_FeatureId]; if (feature == null) throw new SPException("Unable to locate Feature."); if (scope == ActivationScope.Feature) scope = (ActivationScope)Enum.Parse(typeof(ActivationScope), feature.Scope.ToString().ToLowerInvariant(), true); ActivateDeactivateFeatureAtScope(feature, scope, activate, url, force, ignoreNonActive); }