public static void UpgradeFeatures(ref SPWebApplication webApp, Guid featureId) { SPFeatureQueryResultCollection features = null; try { features = webApp.QueryFeatures(featureId, true); } catch {// not contained } if (features != null) { IEnumerator <SPFeature> featureEnumerator = features.GetEnumerator(); featureEnumerator.Reset(); while (featureEnumerator.MoveNext()) { try { SPFeature feature = featureEnumerator.Current; feature.Upgrade(false); } catch (Microsoft.SharePoint.SPFeatureIsOrphanedException oEx) { } } } }
public static void UpgradeFeatures(ref SPSite site, Guid featureId) { SPFeatureQueryResultCollection features = null; try { features = site.QueryFeatures(featureId, true); } catch {// not contained } if (features != null) { IEnumerator <SPFeature> featureEnumerator = features.GetEnumerator(); featureEnumerator.Reset(); while (featureEnumerator.MoveNext()) { SPFeature feature = featureEnumerator.Current; feature.Upgrade(false); } } }
/// <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 UpgradeFeatureInFeatureCollection(SPFeatureCollection features, Guid featureId, bool force) { SPFeature spFeature = null; IEnumerable <Exception> upgradeErrors; bool success = true; string upgradeErrorsAsString = null; spFeature = features[featureId]; var definitionVersion = spFeature.Definition.Version; if (spFeature.Version < definitionVersion) { upgradeErrors = spFeature.Upgrade(force); } else { var errMsg = string.Format("Feature '{0}' does not require upgrade.", featureId); throw new ApplicationException(errMsg); } if (upgradeErrors != null && upgradeErrors.Count() > 0) { success = false; foreach (Exception x in upgradeErrors) { upgradeErrorsAsString += string.Format( "Error: {0}\n", x.Message ); } } if (spFeature.Version != definitionVersion || !success) { var errMsg = string.Format("Feature upgrade for feature '{0}' failed. Feature version: '{1}', definition version: '{2}'.", featureId, spFeature.Version, definitionVersion); if (!force) { errMsg += " You might want to try again with 'force' enabled."; } if (!string.IsNullOrEmpty(upgradeErrorsAsString)) { errMsg += upgradeErrorsAsString; } throw new ApplicationException(errMsg); } return(spFeature); }