Web.config modification is not an easy task, a small mistake can mess all the web.config and application can go dumb, so while modifying the web.config extra precaution is needed. This post is written keeping in mind how you can edit the web.config in most easiest way , by focusing on very common mistakes committed by most.
To modify web.config programmatically you need to use SPWebConfigModification
- it updates columns in the Object table with your values and SharePoint modify web.config itself across all servers. Unfortunately, this method is really poor documented, so you need to Google for the samples.
So here is the code snippet: It shows the entry that modifies the Log4Net entry in the web.config
Public void CreateModification ()
SPWebConfigModification modification = new SPWebConfigModification();
modification.Owner = "OwnerOfTheModification";
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
modification.XPath = "configuration";
modification.Name = "log4net [@debug='true']";
modification.Value = @"<Log4Net>
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<appendToFile value="true" />
<datePattern value="-dddd" />
<file value="c:\Log.txt" />
<conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
<rollingStyle value="Date" />
<appender-ref ref="RollingLogFileAppender" />
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
<level value="ALL" />
Explaination of the code
The Modification requires some important values , if not correctly set can fail the whole process.
Gets or sets the name of the attribute or section node to be modified or created. This is an XPath expression relative to the parent node (specified by the Path property). Sometimes this is nothing more than the name of the new or existing node you want to change. This is to be set wisely because in 90% of the cases the removing process fails because it is not kept correct .
Every modification has an Owner. Every change made the web.config can be assigned an owner. The owner is nothing more than a string value that uniquely identifies your changes from any other changes that have been made, or may be made in the future. So choose wisely the name so that it could remain unique to your entries.
Gets or sets the XPath expression that is used to locate the node that is being modified or created. This value usually contains simple XPath expression to the parent node of the item you intend to change or create. For example: “system.web/httpHandlers” or “system.web/customErrors”.
Gets or sets the value of the item to set. This is usually the complete xml element tag or attributes value for the node specified in the Name property (or as the name parameter in the constructor).
Gets or sets the type of modification for this object instance. The three values available are defined via the SPWebConfigModification. SPWebConfigModificationType and are EnsureChildNode, EnsureAttribute, and EnsureSection. I have used only EnsureChildNode and it works fine. Not for sure but entries made using the EnsureSection cannot be removed.
Gets or sets the sequence number of the modification. I have always used zero (0) for this.
Note: For applying the modifications , we can perform it in two different ways :
Since I have found many Posts saying which one works, but I'll suggest try with both the ways and check it out(for me point 1 works)