The following snippets may be useful when programmatically activating and deactivating features in SharePoint.
To activate a feature:
To deactivate a feature:
The following example installs a feature definition in the server farm
To activate a feature:
// Activate site features SPWeb currentSite = SPContext.Current.Web; try { currentSite.Features.Add(new Guid("{12345678-1234-1234-1234-123456789123}")); } catch { } // Activate site collection features SPSite currentSiteCollection = SPContext.Current.Site; try { currentSiteCollection.Features.Add(new Guid("{12345678-1234-1234-1234-123456789123}")); } catch { }
To deactivate a feature:
// De-activate site features SPWeb currentSite = SPContext.Current.Web; try { currentSite.Features.Remove(new Guid("{12345678-1234-1234-1234-123456789123}")); } catch { } // De-activate site collection features SPSite currentSiteCollection = SPContext.Current.Site; try { currentSiteCollection.Features.Remove(new Guid("{12345678-1234-1234-1234-123456789123}")); } catch { }
The following example installs a feature definition in the server farm
System.Globalization.CultureInfo cInfo = new System.Globalization.CultureInfo(1033); SPFeatureDefinitionCollection farmFeatures = SPFarm.Local.FeatureDefinitions; Guid farmGuid = new Guid("BCFC1029-7DB4-4e9c-B162-B03CE8497DAC"); SPFeatureDefinition featureDefinition = farmFeatures.Add(@"Feature_Folder\Feature.xml", farmGuid, true); Response.Write("Farm definition " + SPEncode.HtmlEncode(featureDefinition.GetTitle(cInfo)) + " added. ");
source: Shailen Sukul
No comments:
Post a Comment