Wednesday, May 28, 2008

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

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.

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com