How To: Use the Object Model to Modify Web.config

In Windows SharePoint Services 3.0, you can modify Web.config settings by creating an XML file that describes the modifications, or you can perform the same task programmatically by using the SPWebConfigModification class of the Microsoft.SharePoint.Administration namespace, which allows you to dynamically register entities.

Example

The following example shows how to use the SPWebConfigModification class to register a custom assembly. For information about how to create an application that implements the example, see Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio.

Visual Basic
Dim service As SPWebService = SPWebService.ContentService

Dim myModification As New SPWebConfigModification()
myModification.Path = "configuration/SharePoint/SafeControls"
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']"
myModification.Sequence = 0
myModification.Owner = "User Name"
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />"
service.WebConfigModifications.Add(myModification)

'Call Update and ApplyWebConfigModifications to save changes
service.Update()
service.ApplyWebConfigModifications()
C#
SPWebService service = SPWebService.ContentService;

SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration/SharePoint/SafeControls";
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']";
myModification.Sequence = 0;
myModification.Owner = "User Name";
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />";
service.WebConfigModifications.Add(myModification);
 
/*Call Update and ApplyWebConfigModifications to save changes*/ 
service.Update();
service.ApplyWebConfigModifications();

In the example, the Name property contains an XPath statement that uniquely identifies the node, which ensures that duplicates of the node are not added to the file.

Calling the ApplyWebConfigModifications method schedules a timer job to deploy the changes throughout the server farm. To apply a Web.config modification to a specific Web application, add the modification to the collection of Web.config modifications for the Web application (WebConfigModifications). For example, you can use oWebSite.Site.WebApplication.WebConfigModifications.Add(MyModification) to add a Web.config modification to the parent Web application of a specific Web site.

Note Note:

You must be an administrator on the front-end Web server for your code to work.

Tags :


Community Content

Thomas Lee
Webconfig modification are not removed from other zones than Default

If you have extended a webapplication to other zones than Default: Extranet, Intranet etc. new modifications are reflected across the zones as expected.

But if you try to remove the webconfig modification changes are only reflected on the Default zone. You have to manually remove the entries for the other zones.

[tfl - 29/6/09] - You should post questions like this to the MSDN Forums at http:/forums.microsoft.com/msdn. You are much more likely get a quick response using the forums than through the Community Content.

Tags :

Noelle Mallory - MSFT
Web config modification updates other web applications web config

Is it possible to modify the web.config of the desired web application ? When I modify the web config, other web configs are modified as well.

[Noelle Mallory - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.

Tags :

Keith Dahlby
Updating a specific Web Application's web.config

To apply changes to a single web application, you can use code like this:

SPWebApplication webApp = oWebSite.Site.WebApplication;
webApp.WebConfigModifications.Add(myModification);
webApp.Update();
SPWebService.ContentService.ApplyWebConfigModifications();


Page view tracker