Modifying web.config
Hi
We're developing a feature for Sharepoint 2007 which require some keys under appSettings in the web.config file. Therefore, I created a webconfig.[uniqueID].xml file which gets copied to the c:\program files\common files\microsoft shared\web server extensions\12\CONFIG folder just before the feature is installed and activated. This seems to work OK - the appSettings keys are added to the web.config file for the site in question under C:\Inetpub\wwwroot\wss\VirtualDirectories - but the next time the feature is updated and activated the appSettings keys specified are added again, causing duplicate entries for several keys. The same goes for all other web.config settings specified in our webconfig.[uniqueID].xml file.
Is this how it's supposed to work, or are we doing anything wrong? Is there some documentation for this to be found anywhere? I wan't it to work like this: The first time the feature is activated, the new keys should be added to the web.config file. However, on further updates they shouldn't be added again (and again), but only updated with new values (if these have changed between the different feature versions).
Here's an example of what our partial web.config file looks like:
<actions>
<update path="configuration/appSettings">
<add key="keyname" value="Some value" />
</update>
</actions>
After a couple of updates of the feature, the appSettings section of the web.config file for the site looks like this:
<appSettings>
<add key="keyname" value="Some value" />
<add key="keyname" value="Some value" />
<add key="keyname" value="Some value" />
<add key="keyname" value="Some value" />
</appSettings>
The problem is that you are doing it in such a way that when the feature is installed, it adds the keys to the config file, so that by design everytime that it is installed, these get added. One alternative is to check for these config entries in the FeatureActivated event of the FeatureReceiver. If they do not exist, then create with the values that you want them to have. If they do exist, then update the values. One side benefit of this apporach is that if you want to update the values in the config, but nothing else about the feature, and the file with the values that you want exists in the Features folder, then you can change the values, then Deactivate, then Activate again the Feature.
I hope this helps.
Hi
Thanks for your answer.
So you're saying that I should add, and modify, web.config settings using the SPWebConfigModification object instead? I suppose that's a possible solution to the problem, but I think that approach makes it harder to keep my web.config settings up to date, compared to using a webconfig.[uniqueId].xml. We're probably going to need tons of various web.config settings so using an .xml file would probably be easier. Also, for some reason it feels better to keep code and web.config settings separated.
I think it's a little strange that Sharepoint just adds a new key even if the key already exists when using the webconfig.[uniqueId].xml technique. Is there a reason it works like that?
What I am suggesting is that you programmatically edit the config file in the FeatureReceiver. First if the file doesn't exist add it, else edit it; then if the key does not exist, add it, else edit it; etc.? That way you maintain ful control over your config file.