DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Using SPWebConfigModification within a Feature Receiver

Posted by PANVEGA on September 2, 2009

Sometimes when developing moduls for SharePoint you have to register these modules in your application web.config. You can easily do this by using the SPWebConfigModification class in combination with a feature receiver.

This class holds modifications that are made to the web.config.

Quite often I have to make some changes in the web.config files of the web applications where my solutions are to be deployed. Manifest.xml files of Sharepoint Solutions helps us with this task providing us with some easy ways to alter the web.config files, unfortunately, we are limited at registering safe controls and alter security policies which sometimes is not enough.

Feature Recievers

First I’ll explain what a feature reciever is. A feature reciever is a class that gets called by a SharePoint feature on 4 specific events. These events are:

  • FeatureInstalled
  • FeatureUninstalling
  • FeatureActivated
  • FeatureDeactivating


I had to replace the default Application.master with a CustomApp.master. The problem with customising the application.master page is that it will affect all sites on your web server farm, rather than just a specific site. SharePoint comes with a rich set of application pages in the /_layouts directory which are primarily used for administrative purposes, i.e. when switching to the Site Settings page. The /_layouts directory is a virtual directory being mapped into each web site, thus all web wsites in a farm refer to the same application pages. This is also the place where our application.master provides.

One way to fix this usue is to write an HttpModule that will intercept the calls to these system pages and replace the application.master page with your sites custom master page.

More about how to create and deploy a CustomApplication.master by using HttpModule you find in my previous post.


It took quite some time to get my HttpModule to register/unregister correctly on activation/deactivation of my web application level feature. First develope your solution and get the dll assembly information by using the reflector or move the dll to your GAC.

Copy the information and replace this code with your one. Finnally build a solutin and deploy it.

Code example:

class MasterReceiver : SPFeatureReceiver
public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
SPWebConfigModification modification = CreateModification();

private void AddWebConfigModifications(SPWebApplication webApp, SPWebConfigModification modification)
// Commit modification additions to the specified web application
// Push modifications through the farm

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
if (webApp != null)

private void RemoveWebConfigModificationsByOwner(SPWebApplication webApp)
Collection<SPWebConfigModification> modificationCollection = webApp.WebConfigModifications;
Collection<SPWebConfigModification> removeCollection = new Collection<SPWebConfigModification>();

int count = modificationCollection.Count;
for (int i = 0; i < count; i++)
SPWebConfigModification modification = modificationCollection[i];
// collect modifications to delete

// now delete the modifications from the web application
if (removeCollection.Count > 0)
foreach (SPWebConfigModification modificationItem in removeCollection)

// Commit modification removals to the specified web application
// Push modifications through the farm
private SPWebConfigModification CreateModification()
SPWebConfigModification modification = new SPWebConfigModification(“add[@name=’CustomApplicationHttpModule’]”, “configuration/system.web/httpModules“);
modification.Owner = “12345678”;
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
modification.Value = @”<add name=””CustomApplicationHttpModule”” type=””Receiver_Application_Master.ApplicationMasterModule, Receiver_Application_Master, Version=, Culture=neutral, PublicKeyToken=XXXXXXXXXX”” />”;
return modification;

  • the CustomApplicationHttpModule can be named soemhow.
  • Every modification has an Owner. Every change made the web.config can be assigned an owner. The owner is nothing more than a string value that uniquely identifies your changes from any other changes that have been made, or may be made in the future.  I suggest using the name of the assembly, or the feature name or feature ID.
  • configuration/system.web/httpModules” .Gets or sets the XPath expression that is used to locate the node that is being modified or created. This value usually contains simple XPath expression to the parent node of the item you intend to change or create.

Here is the solution after activating the feature in the web.config httpModules tag:

<clear />
<add name=”CustomApplicationHttpModule” type=”Receiver_Application_Master.ApplicationMasterModule, Receiver_Application_Master, Version=, Culture=neutral, PublicKeyToken=XXXXXXXXXXX” />

That´s it.

Good luck,

More Information:

One Response to “Using SPWebConfigModification within a Feature Receiver”

  1. […] Using SPWebConfigModification within a Feature Receiver […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: