Description:
The following sample demonstrates using the SPWebConfigModification class to programmatically add and delete an entry to a SharePoint Web Application’s web.config file.
Usage scenario:
In this example we are programmatically adding a safe control entry to the web applications web.config. The SPWebConfigModification has an “Owner” assigned to it. This owner allows tracking of changes made through this API. With an owner specified while adding the SPWebConfigModification can be retrieved using the same user name for later modification or removal.
C# Code Sample
static void AddConfigValueWL()
{
// Get an instance of the web application we want to modify
SPWebApplication spweb = SPWebApplication.Lookup(new Uri("SomeServer"));
// Create a new modification object to store our change
SPWebConfigModification myModification = new SPWebConfigModification();
// Specify the path to the element of the web.config we want to change
myModification.Path = "configuration/SharePoint/SafeControls";
// Specify the element to change via X-Path
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'] [@Safe='True']";
myModification.Sequence = 0;
// Set an owner for the change to track (you can use this value to find the change made and modify/remove it.
myModification.Owner = "testowner";
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
// Specify the value
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />";
spweb.WebConfigModifications.Add(myModification);
// Update and Apply the new settings
spweb.Update();
spweb.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
static void DeleteConfigValueWL()
{
SPWebApplication spweb = SPWebApplication.Lookup(new Uri("http://SomeServer"));
// Get a collection of all web.config changes made via the api
Collection<SPWebConfigModification> modsCollection = spweb.WebConfigModifications;
List<SPWebConfigModification> modsToRemove = new List<SPWebConfigModification>();
// Find all modifications by 'testowner' and save them in a list
foreach (SPWebConfigModification modification in modsCollection)
{
if (modification.Owner == "testowner")
modsToRemove.Add(modification);
}
// Go through the list and remove the changes from the web
foreach (SPWebConfigModification modification in modsToRemove)
{
modsCollection.Remove(modification);
// Update and Apply
spweb.Update();
spweb.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}
VB.NET Example
Sub AddConfigValueWL()
' Get an instance of the web application we want to modify
Dim spweb As SPWebApplication = SPWebApplication.Lookup(New Uri("http://SomeServer"))
' Get a new modification object to store our change
Dim myModification As SPWebConfigModification = New SPWebConfigModification()
' Specify the path to the element of the web.config we want to change
myModification.Path = "configuration/SharePoint/SafeControls"
' Specify the element to change via X-Path
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']"
myModification.Sequence = 0
myModification.Owner = "testowner"
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />"
' Add our change to the web application
spweb.WebConfigModifications.Add(myModification)
' Update and apply the new settings
spweb.Update()
spweb.Farm.Services.GetValue(Of SPWebService)().ApplyWebConfigModifications()
End Sub
Sub DeleteConfigValueWL()
' Get an instance of the web application we want to modify
Dim spweb As SPWebApplication = SPWebApplication.Lookup(New Uri("http://SomeServer"))
' Get a new modification object to store our change
Dim modsCollection As Collection(Of SPWebConfigModification) = spweb.WebConfigModifications
Dim modsCount As Integer = modsCollection.Count
Dim modsToRemove As List(Of SPWebConfigModification) = New List(Of SPWebConfigModification)
' Find all modifications by 'testowner' and save them in a list
For Each modification As SPWebConfigModification In modsCollection
If modification.Owner = "testowner" Then
modsToRemove.Add(modification)
End If
Next
' Go through the list and remove the changes from the web
For Each modification As SPWebConfigModification In modsToRemove
modsCollection.Remove(modification)
' Update and Apply
spweb.Update()
spweb.Farm.Services.GetValue(Of SPWebService)().ApplyWebConfigModifications()
Next
End Sub