Friday, September 10, 2010

SPWebConfigModifications throws exception all time


{Expression must evaluate to a node-set.}
{'' is an invalid expression}

Are you victim of the above error messages?
Is SPWebConfigModification throwing exceptions all the time when modifying the web.config?
And you think it should not give that message for the flawless code that you have written?
then root cause could be because of the corrupted entries in webconfigmodification collection that still persist.
So how to get rid of them?

Call SPWebApplication.WebConfigModifications.Clear() before adding the entries(SPWebApplication.WebConfigurations.Add(EntryItem))

In a very special case of mine the above call didn't succeed and i have to call
SPWebApplication.Farm.Services.GetValue().WebConfigModifications.Clear() too.

So what did we really do just now, The call SPWebApplicaiton.WebConfigModifications.Clear()
cleared the bad entries on the current webapp where we are trying to add the entries.But there
could be bad entries still existing on the other webapps because of which error might be thrown though
there is nothing wrong in your code.
So to clear out all the corrupted entries we have called SPWebApplicaiton.Farm.Services.GetValue().WebConfigModifications.Clear()
Internally this call will reload all the web.config entires and modifies the current webapp
web.config to include the new changes.



  1. thanks for the post. solved a HUGE problem for me.

    The following code is what worked for me.

    SPWebApplication webApp = new SPSite("http://localhost/").WebApplication;


  2. Thanks MJ! This is a big help! Keep up your good works!