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);
            }
        }
Example #2
0
        private static SPFeature SafelyActivateFeature(SPFeatureCollection features, FeatureDefinition featureModel)
        {
            SPFeature result = null;

            try
            {
                result = features.Add(featureModel.Id, featureModel.ForceActivate);
            }
            catch (Exception e)
            {
                // sandbox site/web features?
                // they need to ne activated with SPFeatureDefinitionScope.Site scope
                if ((featureModel.Scope == FeatureDefinitionScope.Site || featureModel.Scope == FeatureDefinitionScope.Web) &&
                    e.Message.ToUpper().Contains(featureModel.Id.ToString("D").ToUpper()))
                {
                    result = features.Add(featureModel.Id, featureModel.ForceActivate, SPFeatureDefinitionScope.Site);
                }
                else
                {
                    throw;
                }
            }

            return(result);
        }
        public void Add(object featureId, object force, object featureDefinitionScope)
        {
            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.Add(featureGuid);
                return;
            }

            if (featureDefinitionScope == null || featureDefinitionScope == Null.Value ||
                featureDefinitionScope == Undefined.Value)
            {
                m_featureCollection.Add(featureGuid, TypeConverter.ToBoolean(force));
                return;
            }

            var strFeatureDefinitionScope = TypeConverter.ToString(featureDefinitionScope);
            SPFeatureDefinitionScope scope;

            strFeatureDefinitionScope.TryParseEnum(true, SPFeatureDefinitionScope.None, out scope);

            m_featureCollection.Add(featureGuid, TypeConverter.ToBoolean(force), scope);
        }
        public static void ActivateFeatureInWeb(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.Add(guidFeatureDefinitionID, true);
                            }
                            web.Dispose();
                        }
                    }
                }
            }
        }
        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;
                        }
                    }
            });
        }
Example #6
0
        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);
        }
Example #7
0
 public static void EnsureActivated(this SPFeatureCollection features, Guid featureId)
 {
     if (!IsActivated(features, featureId))
     {
         features.Add(featureId);
     }
 }
        /// <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>
 /// Safely add new feature : AddFeatureIfNotAlreadyAdded
 /// </summary>
 /// <param name="logger">ILogger logger</param>
 /// <param name="collection">SPFeatureCollection collection</param>
 /// <param name="featureGuid">Guid featureGuid</param>
 private void AddFeatureIfNotAlreadyAdded(ILogger logger, SPFeatureCollection collection, Guid featureGuid)
 {
     if (collection[featureGuid] == null)
     {
         logger.Info("Activating feature with Guid " + featureGuid.ToString());
         collection.Add(featureGuid);
     }
     else
     {
         logger.Info("Skipping feature with Guid " + featureGuid.ToString());
     }
 }
Example #10
0
        /// <summary>
        /// activate 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>
        /// <returns>the activated feature</returns>
        /// <remarks>attention, might throw exception!</remarks>
        internal static SPFeature ActivateFeatureInFeatureCollection(SPFeatureCollection features, Guid featureId, bool force)
        {
            SPFeature spFeature = features.Add(featureId, force);

            if (spFeature == null)
            {
                var errMsg = string.Format("Feature activation for feature '{0}' failed.", featureId);

                if (!force)
                {
                    errMsg += " You might want to try again with 'force' enabled.";
                }

                throw new ApplicationException(errMsg);
            }

            return(spFeature);
        }
        private void EnableFeatures(string site)
        {
            try
            {
                using (SPWeb webNew = new SPSite(site).OpenWeb())
                {
                    bool isRootWeb = webNew.IsRootWeb;
                    if (isRootWeb)
                    {
                        foreach (feature sfeature in siteFeatures)
                        {
                            try
                            {
                                using (SPWeb web = new SPSite(site).OpenWeb())
                                {
                                    SPFeatureCollection siteFeaturesCollection = web.Site.Features;
                                    output.Append(string.Format("activating site feature: {0}" + Environment.NewLine, sfeature.name));
                                    //var feature = siteFeaturesCollection.SingleOrDefault(f => f.DefinitionId == sfeature.definitionId);
                                    //output.Append(string.Format("feature found: {0}" + Environment.NewLine, feature.Definition.DisplayName));
                                    siteFeaturesCollection.Add(sfeature.definitionId, true);
                                }
                            }
                            catch { output.Append(string.Format("error activating site feature: {0}" + Environment.NewLine, sfeature.name)); }
                        }
                    }

                    foreach (feature wfeature in webFeatures)
                    {
                        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("activating web feature: {0}" + Environment.NewLine, wfeature.name));
                                webFeaturesCollection.Add(wfeature.definitionId, true);
                            }
                        }
                        catch { output.Append(string.Format("error activating web feature: {0}" + Environment.NewLine, wfeature.name)); }
                    }
                }
            }
            catch { }
        }
        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);
        }
Example #13
0
 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)
                          ));
     }
 }
Example #14
0
        public static SPFeature ActivateOneFeature(SPFeatureCollection features, Guid featureId, ILog log)
        {
            bool force = true;

#if SP2013_UNUSED
            // Unneeded - 14/15 hive deployment is governed at solution deployment time
            // Full-blown solution, not sandboxed
            SPFeatureDefinitionScope featureScope = SPFeatureDefinitionScope.Farm;
            // Note: 2013-03-20
            // This does not appear to work, at least with an SP2010 wsp
            // it appears to always get activated at level 14
            // This article describes an undocumented workaround
            //  http://www.boostsolutions.com/blog/how-to-install-a-farm-solution-in-2010-and-2013-mode-for-sharepoint-2013/
            if (InstallConfiguration.CompatibilityLevel == "14" ||
                InstallConfiguration.CompatibilityLevel.ToUpper() == "OLD" ||
                InstallConfiguration.CompatibilityLevel.ToUpper() == "OLDVERSION" ||
                InstallConfiguration.CompatibilityLevel.ToUpper() == "OLDVERSIONS"
                )
            {
                log.Info("Activating feature at level 14: " + featureId.ToString());
                return(features.Add(featureId, 14, force, featureScope));
            }
            else if (InstallConfiguration.CompatibilityLevel == "15" ||
                     InstallConfiguration.CompatibilityLevel.ToUpper() == "NEW" ||
                     InstallConfiguration.CompatibilityLevel.ToUpper() == "NEWVERSION" ||
                     InstallConfiguration.CompatibilityLevel.ToUpper() == "NEWVERSIONS"
                     )
            {
                log.Info("Activating feature at level 15: " + featureId.ToString());
                return(features.Add(featureId, 15, force, featureScope));
            }
            else if (InstallConfiguration.CompatibilityLevel == "14,15" ||
                     InstallConfiguration.CompatibilityLevel.ToUpper() == "ALL" ||
                     InstallConfiguration.CompatibilityLevel.ToUpper() == "ALLVERSIONS"
                     )
            {
                log.Info("Activating feature at level 14 then 15: " + featureId.ToString());
                features.Add(featureId, 14, force, featureScope);
                return(features.Add(featureId, 15, force, featureScope));
            }
            else
            {
                return(features.Add(featureId, force));
            }
#else
            return(features.Add(featureId, force));
#endif
        }
        public static void ActivateFeatureInFarm(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.Add(guidFeatureDefinitionID, true);
                    }
                }
            }
        }
Example #16
0
        public ArtDevFeature EnablePublishigFeature()
        {
            using (SPSite site = new SPSite(this.site.Url))
            {
                site.AllowUnsafeUpdates = true;
                //Activate the publishing feature at the site collection level
                SPFeatureCollection sFeatureCollect = site.Features;
                sFeatureCollect.Add(new Guid("F6924D36-2FA8-4f0b-B16D-06B7250180FA"), true);
                site.AllowUnsafeUpdates = false;
            }

            using (SPSite site = new SPSite(this.site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    //Activate the publishing feature at the web level
                    SPFeatureCollection wFeatureCollect = web.Features;
                    wFeatureCollect.Add(new Guid("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb"), true);
                    web.AllowUnsafeUpdates = false;
                }
            }
            return(this);
        }
 public static SPFeature ActivateOneFeature(SPFeatureCollection features, Guid featureId, ILog log)
 {
     bool force = true;
     #if SP2013_UNUSED
     // Unneeded - 14/15 hive deployment is governed at solution deployment time
     // Full-blown solution, not sandboxed
     SPFeatureDefinitionScope featureScope = SPFeatureDefinitionScope.Farm;
     // Note: 2013-03-20
     // This does not appear to work, at least with an SP2010 wsp
     // it appears to always get activated at level 14
     // This article describes an undocumented workaround
     //  http://www.boostsolutions.com/blog/how-to-install-a-farm-solution-in-2010-and-2013-mode-for-sharepoint-2013/
     if (InstallConfiguration.CompatibilityLevel == "14"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "OLD"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "OLDVERSION"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "OLDVERSIONS"
         )
     {
         log.Info("Activating feature at level 14: " + featureId.ToString());
         return features.Add(featureId, 14, force, featureScope);
     }
     else if (InstallConfiguration.CompatibilityLevel == "15"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "NEW"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "NEWVERSION"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "NEWVERSIONS"
         )
     {
         log.Info("Activating feature at level 15: " + featureId.ToString());
         return features.Add(featureId, 15, force, featureScope);
     }
     else if (InstallConfiguration.CompatibilityLevel == "14,15"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "ALL"
         || InstallConfiguration.CompatibilityLevel.ToUpper() == "ALLVERSIONS"
         )
     {
         log.Info("Activating feature at level 14 then 15: " + featureId.ToString());
         features.Add(featureId, 14, force, featureScope);
         return features.Add(featureId, 15, force, featureScope);
     }
     else
     {
         return features.Add(featureId, force);
     }
     #else
     return features.Add(featureId, force);
     #endif
 }
Example #18
0
        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 EnableFeature(ISharePointCommandContext context, FeatureInfo featureID)
        {
            SPFeatureCollection featureCollection = GetFeatureCollectionForFeature(context, featureID);

            featureCollection.Add(featureID.FeatureID, true);
        }
        /// <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 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 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
                    });
                }
            }
        }
        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);
            }
        }
 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);
     }
 }
Example #25
0
        private static SPFeature SafelyActivateFeature(SPFeatureCollection features, FeatureDefinition featureModel)
        {
            SPFeature result = null;

            try
            {
                result = features.Add(featureModel.Id, featureModel.ForceActivate);
            }
            catch (Exception e)
            {
                // sandbox site/web features?
                // they need to ne activated with SPFeatureDefinitionScope.Site scope
                if ((featureModel.Scope == FeatureDefinitionScope.Site || featureModel.Scope == FeatureDefinitionScope.Web)
                    && e.Message.ToUpper().Contains(featureModel.Id.ToString("D").ToUpper()))
                {
                    result = features.Add(featureModel.Id, featureModel.ForceActivate, SPFeatureDefinitionScope.Site);
                }
                else
                {
                    throw;
                }
            }

            return result;
        }
Example #26
0
        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 = 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)
                {
                    TraceService.Verbose((int)LogEventId.ModelProvisionCoreCall, "Feature enabled, but ForceActivate = true. Force activating.");

                    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)
                {
                    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
                    });
                }
            }
        }