Thursday, October 18, 2012

Using SPWebConfigModification

Sometimes you need to add changes to web.config during SharePoint development. This is done by using SPWebConfigModification, see http://msdn.microsoft.com/en-us/library/bb861909.aspx. But there is one very big problem with the functionality of SPWebConfigModification, and that is the ApplyWebConfigModifications method.

If you use Microsoft's example in a feature receiver, which you probably would want to do, the method ApplyWebConfigModifications will, as the comment clearly describes, reapply all the configuration modifications each time it is called. Yes, ladies and gentlemen, lets read the sentence "Reapply all the configuration modifications" again. This means that if you have configuration modifications in different features and added one, all the other configuration modifications will be written once again to the web.config file. The method ApplyWebConfigModifications does not take in to account that the configuration modifications already exist in the web.config file.

The best solution I have found is not to use SPWebConfigModification at all. Manually changing your web.config files seems to be the best way.

If you still want to use SPWebConfigModification, do not try to be fancy and remove your configuration modifications because that does not work either. It is a bug in SharePoint 2010 and will never be fixed according to this thread http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/a77a3524-775c-4d04-9920-5bc831e5607a/.

Yet another day in SharePoint paradise...