Pages

Search This Blog

Friday, May 6, 2011

Modifying Sharepoint Web.Config Programmatically


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>
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<appendToFile value="true" />
<datePattern value="-dddd" />
<file value="c:\Log.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
layout>
<rollingStyle value="Date" />
appender>
<logger name="Web.Logging">
<appender-ref ref="RollingLogFileAppender" />
<level value="ALL" />
logger>
<root>
<appender-ref ref="RollingFileAppender" />
<level value="ALL" />
root>
log4net>";
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
Explaination of the code
The Modification requires some important values , if not correctly set can fail the whole process.
Name
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.
Path
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”.
Value
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).
Type
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.
Sequence
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 :
  1. SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
  2. SPWebApplicationObject.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
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)

2 comments:

  1. ApplyWebConfigModifications() is applying the change to web.configs in all sitecollections(like localhost:80, localhost:3441). Is it possible to control this and apply the changes to one sitecollection?

    ReplyDelete
  2. Hi Anonymous
    ApplyWebConfigModifications() will sync the web.config of all the site collections with the entries present in their respective database and will modify the entries in web.config as well as in the database only for the site collection on which the code is executed, for the changes to be made in a single site collection is not possible.

    ReplyDelete