Example #1
0
        private void activateSelectedFeaturesAcrossSpecifiedScope(SPFeatureScope activationScope, Location currentLocation, FeatureActivator.Action action)
        {
            List<Feature> selectedFeatures = GetSelectedFeatureDefinitions();
            FeatureSet featureSet = new FeatureSet(selectedFeatures);
            string verbnow = "?", verbpast = "?";
            switch (action)
            {
                case FeatureActivator.Action.Activating:
                    verbnow = "activate"; verbpast = "activated";
                    break;
                case FeatureActivator.Action.Deactivating:
                    verbnow = "deactivate"; verbpast = "deactivated";
                    break;
                default:
                    throw new Exception("Invalid action in activateSelectedFeaturesAcrossSpecifiedScope");
            }

            if (selectedFeatures.Count > 10)
            {
                InfoBox(string.Format(
                    "Too many features ({0}) selected; max 10 may be {1} at time",
                    selectedFeatures.Count,
                    verbpast
                    ));
                return;
            }

            if (featureSet.LowestScope < activationScope)
            {
                InfoBox(string.Format(
                    "{0} Feature(s) cannot be {1} at level {2}",
                    featureSet.LowestScope,
                    verbpast,
                    activationScope
                    ));
                return;
            }

            string currentLocName = "?";
            if (activationScope == SPFeatureScope.Farm)
            {
                currentLocName = "Local Farm";
            }
            else
            {
                currentLocName = currentLocation.Name + " -- " + currentLocation.Url;
            }
            string msg;
            msg = string.Format(
                "{0}  the selected {1} features: \n"
                + "{2}"
                + "in the selected {3}: \n\t{4}",
                CultureInfo.CurrentCulture.TextInfo.ToTitleCase(verbnow),
                selectedFeatures.Count,
                GetFeatureSummaries(selectedFeatures, "\t{Name} ({Scope})\n"),
                activationScope,
                currentLocName
                );
            if (!ConfirmBox(msg))
            {
                return;
            }
            FeatureActivator activator = new FeatureActivator(m_featureDb, action, featureSet);
            activator.ExceptionLoggingListeners += new FeatureActivator.ExceptionLoggerHandler(activator_ExceptionLoggingListeners);

            switch (activationScope)
            {
                case SPFeatureScope.Farm:
                    {
                        activator.TraverseActivateFeaturesInFarm();
                    }
                    break;

                case SPFeatureScope.WebApplication:
                    {
                        SPWebApplication webapp = GetCurrentWebApplication();
                        activator.TraverseActivateFeaturesInWebApplication(webapp);
                    }
                    break;

                case SPFeatureScope.Site:
                    {
                        using (SPSite site = OpenCurrentSite())
                        {
                            if (site == null) { return; }
                            try
                            {
                                activator.TraverseActivateFeaturesInSiteCollection(site);
                            }
                            finally
                            {
                                site.Dispose();
                            }
                        }
                    }
                    break;

                case SPFeatureScope.Web:
                    {
                        using (SPSite site = OpenCurrentSite())
                        {
                            if (site == null) { return; }
                            try
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    if (web == null) { return; }
                                    try
                                    {
                                        activator.ActivateFeaturesInWeb(web);
                                    }
                                    finally
                                    {
                                        site.Dispose();
                                    }
                                }
                            }
                            finally
                            {
                                site.Dispose();
                            }
                        }
                    }
                    break;
                default:
                    {
                        msg = "Unknown scope: " + activationScope.ToString();
                        ErrorBox(msg);
                    }
                    break;
            }
            msg = string.Format(
                "{0} Feature(s) {1}.",
                activator.Activations,
                verbpast);
            MessageBox.Show(msg);
            logDateMsg(msg);
        }
Example #2
0
        private void activateSelectedFeaturesAcrossSpecifiedScope(SPFeatureScope activationScope)
        {
            string msgString = string.Empty;

            // TODO - if we checked whether there are any web level ones
            // we could maybe save traversing all the webs
            List<Feature> selectedFeatures = GetSelectedFeatureDefinitions();

            string msg;
            msg = string.Format(
                "Do you really want to activate the selected {0} feature(s) in the selected {1}",
                selectedFeatures.Count,
                activationScope
                );
            if (MessageBox.Show(msgString, "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
            {
                return;
            }
            FeatureActivator activator = new FeatureActivator();
            activator.ExceptionLoggingListeners += new FeatureActivator.ExceptionLoggerHandler(activator_ExceptionLoggingListeners);

            switch (activationScope)
            {
                case SPFeatureScope.Farm:
                    {
                        activator.TraverseActivateFeaturesInFarm(selectedFeatures);
                    }
                    break;

                case SPFeatureScope.WebApplication:
                    {
                        SPWebApplication webapp = GetCurrentWebApplication();
                        activator.TraverseActivateFeaturesInWebApplication(webapp, selectedFeatures);
                    }
                    break;

                case SPFeatureScope.Site:
                    {
                        using (SPSite site = OpenCurrentSite())
                        {
                            if (site == null) { return; }
                            try
                            {
                                activator.TraverseActivateFeaturesInSiteCollection(site, selectedFeatures);
                            }
                            finally
                            {
                                site.Dispose();
                            }
                        }
                    }
                    break;

                case SPFeatureScope.Web:
                    {
                        using (SPSite site = OpenCurrentSite())
                        {
                            if (site == null) { return; }
                            try
                            {
                                using (SPWeb web = site.OpenWeb())
                                {
                                    if (web == null) { return; }
                                    try
                                    {
                                        activator.ActivateFeaturesInWeb(web, selectedFeatures);
                                    }
                                    finally
                                    {
                                        site.Dispose();
                                    }
                                }
                            }
                            finally
                            {
                                site.Dispose();
                            }
                        }
                    }
                    break;
                default:
                    {
                        msg = "Unknown scope: " + activationScope.ToString();
                        MessageBox.Show(msg, "Warning!");
                        logDateMsg(msg);
                    }
                    break;
            }
            int featuresActivated = activator.Activations;
            msg = string.Format(
                "{0} Feature(s) were/was activated.",
                featuresActivated);
            MessageBox.Show(msgString);
            logDateMsg(msgString);
        }