SPWebConfigModification Best Practices and Guidelines

The SPWebConfigModification class allows developers to write code that will make modifications to the web.config files of SharePoint sites. This is quite often necessary when you want to deply SharePoint customizations together with configuration settings. Using the SPWebModification class is not without any danger! :-) You can mess up your web.config files especially if you don't think about how you want to be able to undo your changes. Mark Wagner wrote an excellent article about the best practices related to the SPWebConfigModification class; required reading for every SharePoint developer. Mark explains some very important things you should know to get started:

  • To save the modifications, you must use of the following code:
    myWebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
    myWebApp.Update();
  • Only call this code once in your feature handler, otherwise you get the error "A web configuration modification operation is already running."

Great info Mark, and please do create an complete example of the best practices, packaged in a feature!

4 Comments

  • Hey Jan,

    Do you think it would be possible to programmatically modify the web.config to add all the Microsoft AJAX extensions?

    I think it would be... just it would be rather err... tedious to code ;)

  • If you are writing a feature or a web part wouldn't it be a better idea to use VSEWSS1.1 or STSDEV, so that you never have to touch this class at all? It just sounds like one of those scary things you could mess up, so why touch if you really don't need to with all the great tools out there?

  • @Aaron, yes it is possible and yes it would require some boring code. :-) But I'd rather write that code once, instead of having to change the web.config hundreds of times!

    @Becky, I don't see how VSEWSS or STSDEV would help in the scenarios where you would like to use SPWebConfigModification. You don't make use of this SPWebConfigModification when you want to add a SafeControl entry of course, the WSP deployment has to do this.

  • @Jan - Well if you were to write the boring code I'm sure us in the community would be more than appreciative of it ;)

Comments have been disabled for this content.