Disappearing web.config entries

How many times have you experienced a chilling moment when something goes terribly wrong with the system you just touched and you don't have any clue what would've caused it?

In a SharePoint installation with multiple web applications and several custom solutions there may be a lot of action going on in web.config files. Even the slightest validation error in these files will bring the web application to a halt. This and the fact that the SPWebConfigModification class has a will on its own make the task of coordinating web.config modifications a very touchy business.

Recently one of my colleagues reported that after installing one of the SharePoint solutions, entries installed by another solution were disappearing, leaving the web application in chaos. Logically I started poking the features in the SharePoint solution, which was "causing" the issue, but this lead me to no where.  I only learned that when you call:


The web.config files for all web applications get rewritten, regardless of which web application is being updated. But this turned out to be a "feature" of SharePoint. Then I started investigating what other web.config modifications are being created by the rest of the solutions on this server. Luckily most of these belong to our company, so I was able to pull up the code. All features worked correctly when executed separately, but still in a particular sequence some of the web.config modifications were disappearing. And there it was ... one of the features was adding the modifications correctly:

SPWebConfigModification modification = new SPWebConfigModification();
modification.Path = "...some path..."
modification.Name = "Example"
modification.Value = value;
modification.Owner = "Owner"
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;

then applying the changes to update the web.config:


But there was no webApp.Update() to persist the changes in the SP database!

It turns out it is very easy to omit this part, because when you develop or debug such feature all will work fine until something does not flush the application pool thus disposing off the newly created SPWebConfigModification. The next solution or feature that calls ApplyWebConfigModifications will force reapply all modifications pulling them from the SharePoint database. For some features this actually might be a welcome side effect, but unless this is not the case you need to call webApp.Update() to permanently save the modifications to the SharePoint database.

One mystery solved. Next, please!



Anonymous said…
Thanks for the post,
I tried the same cose from the blog to update the enteries in the web.config file. It works well in single server environment. In my test environment there are two web front end server. In this case the piece of code doesn't work.

Any inputs on the issue.

Unknown said…
when i am executing the
this._chosenWebApp.Farm.Services.GetValue < SPWebService >().ApplyWebConfigModifications();
it's give me below error.

Expression must evaluate to a node-set.

I have try to by pass and just to update it but still it's having error in this line.

Please help me our with this issue.

Mikhail Dikov said…
It looks like you either have a modification that's not well formed or you have a persisted modification from a past update that's bad.

One option is to clear the modifications collection and then execute ApplyWebConfigModifications(). Another is to load the web config modification feature from this project (http://www.codeplex.com/features) and try to inspect which one is bad.
Anonymous said…
In our Sharepoint farm, we do regular releases. If we want to change web.config values in different releases, should we stick to one feature and keep activating and deactivating it with the latest web.config modifications ,or should we have separate features everytime. Activating and deactivating features appears to be the best solution, but wanted to know if there was any drawbacks to this approach
Sam said…
It usually happens to me. I just don't know what to do. Thanks for giving me an idea on what to do.

web design Perth
Andrea Fox said…
At last, I have found a solution to my problem. All I have to do is call webApp.Farm.Services.GetValue().ApplyWebConfigModifications(); to fix the problem.

web design norwich