Sep 20, 2012

Change a web.config entry using SPWebConfigModification

At first glance, it's not completely obvious how to change an existing entry in the web.config using the SPWebConfigModification class. As it turns out, the key is in the Name attribute, which is actually an XPath expression. Most examples show how to add a new entry, so the Name and Value attributes are the same.

 Example of how to change an existing entry 

myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='False']";

myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />"; 

This will change an entry matching Name with Safe='False' to Safe='True'. If you wanted a more relaxed filter to match regardless of the Safe value, simply remove that part of the Name.

myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*']";