Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (1)
Collapse All/Expand All Collapse All
This page is specific to
The 2010 product release

Other versions are also available for the following:
SPWebConfigModification Class

Holds modifications that are made to the web.config.

System..::.Object
  Microsoft.SharePoint.Administration..::.SPAutoSerializingObject
    Microsoft.SharePoint.Administration..::.SPWebConfigModification

Namespace:  Microsoft.SharePoint.Administration
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
Visual Basic (Declaration)
<GuidAttribute("2BE8CC87-138D-4A5F-96A7-A842D6364FA4")> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
Public NotInheritable Class SPWebConfigModification _
    Inherits SPAutoSerializingObject
Visual Basic (Usage)
Dim instance As SPWebConfigModification
C#
[GuidAttribute("2BE8CC87-138D-4A5F-96A7-A842D6364FA4")]
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
public sealed class SPWebConfigModification : SPAutoSerializingObject

A collection of web.config modifications is a set of commands that, when processed by the web.config manipulator in Microsoft SharePoint Foundation, change the state of the web.config file. You can string together a set of these commands to ensure that they apply the desired tags and attributes within web.config. Each modification is expressed as an object in the administrative object model.

Use the WebConfigModifications property of the SPWebApplication or SPWebService class to get the collection of web.config modifications either in the Web application or in all Web applications within the Web service. To apply modifications that you define through the SPWebConfigModification class to the web.config files in the server farm, call the ApplyWebConfigModifications method on the current content Web service object, as follows: SPWebService.ContentService.ApplyWebConfigModifications

The following example adds a safe control entry to the safe controls section in web.config throughout the server farm.

Visual Basic
Dim myService 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 = WebConfigModificationFeatureReceiver.OwnerId
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />"
         
myService.WebConfigModifications.Add(myModification)
myService.Update()
myService.ApplyWebConfigModifications()
C#
SPWebService myService = 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 = WebConfigModificationFeatureReceiver.OwnerId;
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />";

myService.WebConfigModifications.Add(myModification);
myService.Update(); 
myService.ApplyWebConfigModifications();
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Still very dangerous to use      Michael Stum ... Чернявский Алексей Владимирович   |   Edit   |   Show History
@spyhunter73 No, it hasn't. It is still prone to destroy your web.config. For example, on my setup if completely kills the configSections section when I retract a feature, most likely due to some stale cache/config
Tags What's this?: Add a tag
Flag as ContentBug
Easy to use wrapper class here      klinkby   |   Edit   |   Show History
See http://kli.dk/2010/12/10/sharepoint-web-config-modifications/ for a safe implementation on how to merge your own app.config file into SharePoint with a few code lines. Also supports automatic removing.
Tags What's this?: Add a tag
Flag as ContentBug
Interesting additional MSDN content      Bram de Jager - SPRebel   |   Edit   |   Show History
Here is some more information regarding SPWebConfigModification class and code examples for adding and removing. More information: How to: Add and Remove Virtual Web.config Settings, http://msdn.microsoft.com/en-us/library/bb861909(v=office.14).aspx.

An alternative is creating a webconfig.myName.xml in de 12/CONFIG folder. These actions will be merged to all web applications in the farm. Manually pushing the actions is done by using stsadm.exe -o copyappbincontent. More information: How to: Create a Supplemental .config File, http://msdn.microsoft.com/en-us/library/ms439965.aspx.
Tags What's this?: Add a tag
Flag as ContentBug
Has this been updated from 2007?      spyhunter73   |   Edit   |   Show History
The 2007 SPWebConfigModification is a high risk class to use for web parts that require custom configuration settings, such as WCF connection information. The 2007 was nortorously easy to break and was very buggy when used within a feature reciever. Usage from outside of the SharePoint context was strangely more reliable.

Has this been updated at all for 2010 version?
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker