/// <summary> /// try to remove the feature from the farm in all feature collections with this scope /// this only needs to run, when a feature definition should be uninstalled, and it has an error, /// so that it is not clear, which scope the feature definition was defined for. /// </summary> /// <param name="featureID">feature guid of the feature to be removed</param> /// <param name="scope">scope of all the feature collections in the farm to be scanned</param> /// <returns>was the feature found, if yes, it had this scope and other scopes don't need to be scanned</returns> private bool tryToRemove(Guid featureID, SPFeatureScope scope) { string msgString = string.Empty; int featurefound; featurefound = parentWindow.removeFeaturesWithinFarm(featureID, scope); /// search all webs if (featurefound == 0) { msgString = "Search through all Scopes:'" + scope.ToString() + "' done..., feature not found."; parentWindow.logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - " + msgString + Environment.NewLine); return(false); } else { msgString = "Success! Feature was found and deactivated " + featurefound + " times in Scope:'" + scope.ToString() + "'!"; MessageBox.Show(msgString); parentWindow.logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - " + msgString + Environment.NewLine); return(true); } }
/// <summary> /// try to remove the feature from the farm in all feature collections with this scope /// this only needs to run, when a feature definition should be uninstalled, and it has an error, /// so that it is not clear, which scope the feature definition was defined for. /// </summary> /// <param name="featureID">feature guid of the feature to be removed</param> /// <param name="scope">scope of all the feature collections in the farm to be scanned</param> /// <returns>was the feature found, if yes, it had this scope and other scopes don't need to be scanned</returns> private bool tryToRemove(Guid featureID, SPFeatureScope scope) { string msgString = string.Empty; int featurefound; featurefound = parentWindow.RemoveFeaturesWithinFarm(featureID, scope); /// search all webs if (featurefound == 0) { msgString = "Search through all Scopes:'" + scope.ToString() + "' done..., feature not found."; Log.Information(msgString); return(false); } else { msgString = "Success! Feature was found and deactivated " + featurefound + " times in Scope:'" + scope.ToString() + "'!"; MessageBox.Show(msgString); Log.Information(msgString); return(true); } }
/// <summary>search for the feature and deactivate it in the specified scoped feature collections only</summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnScopeSelected_Click(object sender, EventArgs e) { string msgString = string.Empty; int featurefound = 0; this.Hide(); try { if (radioScopeWeb.Checked) { scopeWindowScope = SPFeatureScope.Web; } else { if (radioScopeSite.Checked) { scopeWindowScope = SPFeatureScope.Site; } else { if (radioScopeWebApp.Checked) { scopeWindowScope = SPFeatureScope.WebApplication; } else { if (radioScopeFarm.Checked) { scopeWindowScope = SPFeatureScope.Farm; } else { msgString = "Error in scope selection! Task canceled"; } MessageBox.Show(msgString); ((FrmMain)parentWindow).logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - " + msgString + Environment.NewLine); return; } } } featurefound = parentWindow.removeFeaturesWithinFarm(featureID, scopeWindowScope); if (featurefound == 0) { msgString = "Feature not found in Scope:'" + scopeWindowScope.ToString() + "'. Do you want to try something else?"; if (MessageBox.Show(msgString, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes) { this.Show(); } else { } } else { msgString = "Success! Feature was found and deactivated " + featurefound + " times in Scope:'" + scopeWindowScope.ToString() + "'!"; MessageBox.Show(msgString); parentWindow.logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - " + msgString + Environment.NewLine); SPFarm.Local.FeatureDefinitions.Remove(featureID, true); parentWindow.logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - FeatureDefinition was uninstalled." + Environment.NewLine); } } catch { } finally { this.Close(); this.Dispose(); } }
/// <summary>removes the defined feature within a complete farm</summary> /// <param name="featureID"></param> /// <param name="trueForSPWeb"></param> /// <returns>number of deleted features</returns> public int removeFeaturesWithinFarm(Guid featureID, SPFeatureScope featureScope) { int removedFeatures = 0; int scannedThrough = 0; string msgString; SPSecurity.RunWithElevatedPrivileges(delegate() { msgString = "Removing Feature '" + featureID.ToString() + ", Scope: " + featureScope.ToString() + "' from the Farm."; logDateMsg("Farm - " + msgString); if (featureScope == SPFeatureScope.Farm) { try { SPWebService.ContentService.Features.Remove(featureID, true); removedFeatures++; logDateMsg( string.Format("Success removing feature {0} from {1}", featureID, LocationManager.SafeDescribeObject(SPFarm.Local))); } catch (Exception exc) { logException(exc, string.Format("Exception removing feature {0} from farm", featureID, LocationManager.SafeDescribeObject(SPFarm.Local))); logDateMsg("Farm - The Farm Scoped feature '" + featureID.ToString() + "' was not found. "); } } else { // all the content WebApplications SPWebApplicationCollection webApplicationCollection = SPWebService.ContentService.WebApplications; // administrative WebApplications // SPWebApplicationCollection webApplicationCollection = SPWebService.AdministrationService.WebApplications; foreach (SPWebApplication webApplication in webApplicationCollection) { removedFeatures += removeFeaturesWithinWebApp(webApplication, featureID, featureScope); scannedThrough++; } } msgString = removedFeatures + " Features removed in the Farm. " + scannedThrough + " Web Applications were scanned."; logDateMsg("Farm - " + msgString); }); return removedFeatures; }
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); }
/// <summary>searches for faulty features and provides the option to remove them</summary> /// <param name="features">SPFeatureCollection, the container for the features</param> /// <param name="scope">is needed, in case a feature is found, so that it can be deleted</param> /// <returns></returns> private bool findFaultyFeatureInCollection(SPFeatureCollection features, SPFeatureScope scope) { bool faultyFound = false; if (features == null) { logDateMsg("ERROR: Feature Collection was null!"); return false; } if (features.Count == 0) { logDateMsg("ERROR: Feature Collection was empty!"); return false; } // string DBName = string.Empty; // tbd: retrieve the database name of the featureCollection string featuresName = features.ToString(); try { foreach (SPFeature feature in features) { FeatureChecker checker = new FeatureChecker(); FeatureChecker.Status status = checker.CheckFeature(feature); if (status.Faulty) { faultyFound = true; string location = LocationInfo.SafeDescribeObject(feature.Parent); string msgString = "Faulty Feature found! Id=" + feature.DefinitionId.ToString(); #if SP2013 msgString += " Activation=" + feature.TimeActivated.ToString("yyyy-MM-dd"); #endif string solutionInfo = GetFeatureSolutionInfo(feature); if (!string.IsNullOrEmpty(solutionInfo)) { msgString += solutionInfo; } msgString += Environment.NewLine + "Found in " + location + "." + Environment.NewLine + " Should it be removed from the farm?"; logDateMsg(msgString); string caption = "Found Faulty Feature"; DialogResult response = MessageBox.Show(msgString, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (response == DialogResult.Yes) { removeFeaturesWithinFarm(feature.DefinitionId, scope); } if (response == DialogResult.Cancel) { return faultyFound; } } } } catch (Exception ex) { if (ex is SqlException) { string msgstring = string.Format("Cannot access a feature collection of scope '{0}'! Not enough access rights for a content DB on SQL Server! dbOwner rights are recommended. Please read the following error message:\n\n'{1}'", scope.ToString(), ex.ToString()); string MessageCaption = string.Format("FeatureCollection in a Content DB not accessible"); if(MessageBox.Show(msgstring, MessageCaption,MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return faultyFound; } } else { MessageBox.Show(ex.ToString(), "An error has occured!", MessageBoxButtons.OK); } return faultyFound; } return faultyFound; }
/// <summary> /// try to remove the feature from the farm in all feature collections with this scope /// this only needs to run, when a feature definition should be uninstalled, and it has an error, /// so that it is not clear, which scope the feature definition was defined for. /// </summary> /// <param name="featureID">feature guid of the feature to be removed</param> /// <param name="scope">scope of all the feature collections in the farm to be scanned</param> /// <returns>was the feature found, if yes, it had this scope and other scopes don't need to be scanned</returns> private bool tryToRemove(Guid featureID, SPFeatureScope scope) { string msgString = string.Empty; int featurefound; featurefound = parentWindow.removeFeaturesWithinFarm(featureID, scope); /// search all webs if (featurefound == 0) { msgString = "Search through all Scopes:'" + scope.ToString() + "' done..., feature not found."; parentWindow.logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - " + msgString + Environment.NewLine); return false; } else { msgString = "Success! Feature was found and deactivated " + featurefound + " times in Scope:'" + scope.ToString() + "'!"; MessageBox.Show(msgString); parentWindow.logTxt(DateTime.Now.ToString(FrmMain.DATETIMEFORMAT) + " - " + msgString + Environment.NewLine); return true; } }
private void AddLocationItemToDisplay(SPFeatureScope scope, string webappTitle, string siteTitle, string webTitle, int featureCount) { string scopeString = (scope == SPFeatureScope.WebApplication ? "WebApp" : scope.ToString()); ListViewItem item = new ListViewItem(scopeString); item.SubItems.Add(webappTitle); item.SubItems.Add(siteTitle); item.SubItems.Add(webTitle); if (InstallConfiguration.FeatureIdList.Count > 1) { item.SubItems.Add(featureCount.ToString()); } myList.Items.Add(item); }
/// <summary>searches for faulty features and provides the option to remove them</summary> /// <param name="features">SPFeatureCollection, the container for the features</param> /// <param name="scope">is needed, in case a feature is found, so that it can be deleted</param> /// <returns></returns> private bool findFaultyFeatureInCollection(SPFeatureCollection features, SPFeatureScope scope) { bool faultyFound = false; if (features == null) { logDateMsg("ERROR: Feature Collection was null!"); return false; } if (features.Count == 0) { logDateMsg("ERROR: Feature Collection was empty!"); return false; } // string DBName = string.Empty; // tbd: retrieve the database name of the featureCollection string featuresName = features.ToString(); try { foreach (SPFeature feature in features) { FeatureChecker checker = new FeatureChecker(); FeatureChecker.Status status = checker.CheckFeature(feature); if (status.Faulty) { faultyFound = true; string msgString = DescribeFeatureAndLocation(feature); logDateMsg(msgString); string caption = string.Format("Found Faulty {0} Feature", scope); DialogResult response = MessageBox.Show(msgString, caption, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (response == DialogResult.Yes) { removeFeaturesWithinFarm(feature.DefinitionId, scope); } if (response == DialogResult.Cancel) { return faultyFound; } } } } catch (Exception ex) { if (ex is SqlException) { string msgstring = string.Format("Cannot access a feature collection of scope '{0}'! Not enough access rights for a content DB on SQL Server! dbOwner rights are recommended. Please read the following error message:\n\n'{1}'", scope.ToString(), ex.ToString()); string MessageCaption = string.Format("FeatureCollection in a Content DB not accessible"); if(MessageBox.Show(msgstring, MessageCaption,MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return faultyFound; } } else { MessageBox.Show(ex.ToString(), "An error has occured!", MessageBoxButtons.OK); } return faultyFound; } return faultyFound; }
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); }