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='*']";

2 comments:

  1. Hi Lars,

    Thanks for this post. Yeah it helped me to modified one attribute. But with this, when i deactivate the feature it do not remove the modified item as the name format does not match with the now modified item.

    ReplyDelete
  2. Hi Sumeet,

    Did you try relaxing the Name filter by removing the "[@Safe='False']" part or your modified value?

    ReplyDelete