Пример #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);
        }
 public FeatureActivator(FeatureDatabase featureDb, Action action, FeatureSet featureSet)
 {
     _featureDb = featureDb;
     _action = action;
     _featureset = featureSet;
 }
 public FeatureActivator(FeatureDatabase featureDb, Action action, FeatureSet featureSet)
 {
     _featureDb  = featureDb;
     _action     = action;
     _featureset = featureSet;
 }