How to Modify the web.config file in SharePoint using SPWebConfigModification?

From:http://www.sharepointkings.com/2008/05/how-to-modify-webconfig-file-in.html
 
Hi All,

there is one very interesting class that can be used to modify web application's web.config class.

SPWebConfigModification is the class that can be used to accomplish this kind of work.

The SPWebApplication class has a SPWebConfigModifications collection property that contains all the modifications (SPWebConfigModification objects) made to this web application's web.config file. This is also where we can add new modifications and have them applied to the web.config – or remove them.

Here is a small example which demonstrate you the functionality.

Here is a sample of what the web.config..

<configuration>
<system.web>
<customErrors mode="On">
<system.web>
<configuration>


Here is a sample example which does this task of changing the attribute to a value of "Off".


private void SimpleSample()
{
// Get an instance of my local web application
SPWebApplication webApp = new SPSite("http://localhost").WebApplication;

// Create my new modification to set the mode attibute to "Off".
// Example:
SPWebConfigModification modification = new SPWebConfigModification("mode", "system.web/customErrors");
modification.Owner = "SimpleSampleUniqueOwnerValue";
modification.Sequence = 0;
modification.Type = PWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
modification.Value = "Off";

// Add my new web.config modification.
webApp.WebConfigModifications.Add(modification);

// Save web.config changes.
webApp.Farm.Services.GetValue().ApplyWebConfigModifications();

// Serialize the web application state and propagate changes across the farm.
webApp.Update();
}


That's it. Your job is done.

This is seriously something which is very interesting. As i will find some more methods and more details, i will sure put my knowledge regarding this.
posted @ 2008-10-24 23:11  laputa'sky  阅读(249)  评论(0编辑  收藏  举报