Tuesday, December 28, 2010

Add entries to web.config using wsp

In order to use your solution to add web.config entries follow the below approach-


Step 1: Create feature folder

Step 2: Create Feature.xml file -

Feature Id="113AEEBD-97B0-411a-B118-F6FE6D232C5E"
Title="IMWEBCONFIG"
Description="This feature modifies the Web Application web.config"
Scope="WebApplication"
ReceiverAssembly="Eversheds.InstantMessaging, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=b0c7276684f0a390 "
ReceiverClass="Eversheds.InstantMessaging.FeatureReceiver.ReceiverWebConfig"
xmlns="http://schemas.microsoft.com/sharepoint/"
/Feature

Step 3: Create feature receiver class
SPWebConfigModification webConfigModifications;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

var oWebApp = properties.Feature.Parent as Microsoft.SharePoint.Administration.SPWebApplication;

try
{
if (!oWebApp.IsAdministrationWebApplication)
{
ModifyWebConfigEntries(oWebApp);
}
}
catch (Exception ex)
{
using (StreamWriter w = new StreamWriter("C:\\log.txt"))
{
w.WriteLine(ex.ToString());
}
}
}

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 = @"";
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();
}

No comments:

Post a Comment