WebConfigModifications not persisted

Dec 16, 2009 at 3:27 PM

the WebConfigModifications are not being persisted to the SharePoint databasecausing them to be lost the next time ApplyWebConfigModifications is called.

the fix is simple enough, you just need to add a call to webApp.WebConfigModifications.Update in the Feature receiver on activation/deativation... see edits in bold below

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp != null)
{
RemoveModifications(webApp);
AddModifications(webApp);
webApp.WebConfigModifications.Update();
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}

}

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp != null)
{
RemoveModifications(webApp);
webApp.WebConfigModifications.Update();
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}

Jason

Jan 14, 2010 at 8:16 PM

The suggestion to add webApp.WebConfigModifications.Update();  as mentioned above is incorrect (won't compile either).

The change should be:

       public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
            if (webApp != null)
            {
                RemoveModifications(webApp);
                AddModifications(webApp);                
                SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                webApp.Update();
            }
        }

 Add a similar line to the feature deactivating event.