共用方式為


How to: Add and Remove Web.config Settings Programmatically

Applies to: SharePoint Foundation 2010

In Microsoft SharePoint Foundation, one way to modify web.config settings is to use the SPWebConfigModification class of the Microsoft.SharePoint.Administration namespace, which allows you to dynamically register entities. These modifications are persisted in the configuration database where they function as a kind of virtual web.config that effectively serves as the final layer of the .config file stack for the SharePoint Foundation web application. The changes become effective when the SPWebService.ApplyWebConfigModifications method is called.

Tip

Sometimes these changes are also written to the physical web.config file, but SharePoint Foundation is not consistent in writing modifications to the physical file, so that file will not always reflect every SPWebConfigModification object that has been applied. When troubleshooting web.config modifications, examine the SPWebApplication.WebConfigModifications and SPWebService.WebConfigModifications properties as well as the physical file.

Note

Code that calls ApplyWebConfigModifications works only if it runs in the user context of an administrator on the front-end web server.

Note

For information about a second way of extending web.config files, see How to: Create a Supplemental .config File.

Example: Adding a Setting

The following example shows how to use the SPWebConfigModification class to register a custom assembly.

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. You must still call ApplyWebConfigModifications even if you add a web.config modification to a single Web application.

Removing Configuration Settings

The code to remove a configuration setting is similar, except that your modification is to remove a configuration setting. The following example shows how to remove a configuration setting. Notice that your code must still call ApplyWebConfigModifications

SPWebConfigModification configModFound = null;
SPWebApplication webApplication = SPWebApplication.Lookup(new Uri("https://localhost/"));
Collection<SPWebConfigModification> modsCollection = webApplication.WebConfigModifications;

// Find the most recent modification of a specified owner
int modsCount1 = modsCollection.Count;
for (int i = modsCount1 - 1; i > -1; i--)
{
    if (modsCollection[i].Owner == "User Name")
    {
        configModFound = modsCollection[i];
    }
}

// Remove it and save the change to the configuration database  
modsCollection.Remove(configModFound);
webApplication.Update();

// Reapply all the configuration modifications
webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

See Also

Tasks

How to: Create a Supplemental .config File

Concepts

Working with Web.config Files