Share via


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

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

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.

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()
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

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