Modifying Web.config programmatically
Often we need to add entries in web.config either for Webparts , Http Module ,Connection string or for any other module.For this either we need to add entries manually or we can automate this process using code.We can provide the user an option of On/Off for adding/removing the entries from the web.config . This can be done using feature and associating the feature receiver which will add the entries on Feature Activated and removes the entries on Deactivation of feature.
For this we will use the “SPWebConfigModification” class which will access the web.config of the web application through object model.
Objective: We will insert the HttpModule entries in the Web.config under the ‘HttpModule’ section
Steps are follows as:
Create a feature With scope as WebApplication/Site/Web. Here we will create the feature with scope as WebApplication.
In the Feature.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature Id="{}"
Title="HTTP Module Configuration"
Description="This feature modifies the Web Application web.config to insert the HTTP Module entries."
Scope="WebApplication"
ReceiverAssembly="’AssemblyName’, Version=1.0.0.0, Culture=Neutral, PublicKeyToken= "
ReceiverClass="’ClassName’"
xmlns="http://schemas.microsoft.com/sharepoint/">
</Feature>
Note: Add the feature Receiver class reference in the feature.xml
FeatureReceiver Class code: FeatureActivated method
In this method we will add the entries in the Web.config
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var oWebApp = properties.Feature.Parent as SPWebApplication;
try
{
if (!oWebApp.IsAdministrationWebApplication)
{
ModifyWebConfigEntries(oWebApp);
}
}
catch (Exception ex)
{
}
}
‘ModifyWebConfigEntries’ Method Code :
public void ModifyWebConfigEntries(SPWebApplication oWebApp)
{
#region Http Module Section
webConfigModifications = new SPWebConfigModification();
webConfigModifications.Path = "configuration/system.web/httpModules";
webConfigModifications.Name = "add[@name='CustomHttpModule']";
webConfigModifications.Sequence = 0;
webConfigModifications.Owner = "addCustomModule";
webConfigModifications.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
webConfigModifications.Value = @"<add name='CustomHttpModule' type='ClassName,AssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cfba5cfbc4661d2d' />";
oWebApp.WebConfigModifications.Add(webConfigModifications);
#endregion
SPWebService.ContentService.WebApplications[oWebApp.Id].Update();
//Applies the web config settings in all the web application in the farm
SPWebService.ContentService.WebApplications[oWebApp.Id].WebService.ApplyWebConfigModifications();
}
Note : This ApplyWebConfigModifications() method applies changes in all the web.config which are under the same farm.
Few things to note :
Path : This is the node under which we need to add the entry
Name : Name property is very useful because while retrieving from the database this entry should be in correct format.
Owner : This property is used while deleting the entries from the web.config programmatically
Now we have added the entries in the web.config programmatically.we need to remove these entries also when the feature is no longer in use.
While removing the entries we will take all the entries based on the owner property and delete the required entries.
var oWebApp = properties.Feature.Parent as SPWebApplication;
try
{
if (!oWebApp.IsAdministrationWebApplication)
{
// Web application is not central administration. Configuration changes are different.
DeleteWebConfigEntries(oWebApp);
}
}
catch (Exception ex)
{
}
‘DeleteWebConfigEntries’ Method :
public void DeleteWebConfigEntries(SPWebApplication oWebApp)
{
Collection<SPWebConfigModification> oCollection = oWebApp.WebConfigModifications;
int iStartCount = oCollection.Count;
for (int c = iStartCount - 1; c >= 0; c--)
{
SPWebConfigModification oModification = oCollection[c];
if (oModification.Owner == "addCustomModule")
oCollection.Remove(oModification);
}
if (iStartCount > oCollection.Count)
{
oWebApp.Update();
//Applies the web config settings in all the web application in the farm
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}
Note :Here We are deleting the entries based on the owner property of the Modified entry.
First we take the collection of all the modifications in the Web.config through WebConfigModification method and then remove these entries.
So your job is done
Important thing to remember is that if we are adding the entries in the web.config programmatically then we should delete those entries progrmmatically only because while adding entries throug object model ,entries goes into the content DB of the site and on removing those entries manually this can cause conflict in the version which exist in the file structure and the version in the content DB.
Here is very good article from MSDN which explain this class in detail.
Happy coding J
No comments:
Post a Comment