Lars Nielsen's Discoveries

March 10, 2010

Using SPWebModification to modify the web.config (Part 4)

Filed under: Development,SharePoint — Lars Nielsen @ 10:56 pm
Tags: ,

This is one of a series of posts – start here to read the whole series

In this post I’ll look the ConfigModification class which actually does the work of talking to the SPWebModification class to handle changes to the web.config file.  In order to understand how this class works you do need to be familiar with SPWebModification – a good place to start is here where there’s a good deal of useful information in the comments under the article.  It’s also important to be aware of the limitations to SPWebModification but I’ve not found these to be too onerous.

All the classes described so far leverage the base class ConfigModification, which represents in abstract form a general modification to the web.config file.  I thought about having this class inherit from SPWebModification, but in the end it was simpler to start with my own base class.  ConfigModification has 3 read-only properties.

public string ChangesOwner
public string XPathLocation
public SPWebConfigModification.SPWebConfigModificationType ModificationType

These are all values that I covered in earlier posts and are required to create an SPWebModification object.

The constructor intialises these 3 properties.  You can check this back with the ElementModification and AttributeModification classes I described already:

public ConfigModification(string owner, SPWebConfigModification.SPWebConfigModificationType type, string xPathLocation)
    _changesOwner = owner;
    _modificationType = type;
    _xpathLocation = xPathLocation;

There is a pair of abstract methods, one for the modification name and the other for the value:

public abstract string GetModificationName();

public abstract string GetModificationValue();

The method take no arguments so the could be abstract read-only properties, but then you would have to put all the logic in the property and somehow I don’t expect to find properties with logic in them.

Finally there are the two classes that are called a lot to do the work.  AddToWebApplication creates an SPWebModification object and adds it to the list of modifications for a particular web application.  Remember, the changes are queued up and written onto the web.config file later (I’ll look at that in the next post).

public void AddToWebApplication(SPWebApplication target)
    SPWebConfigModification modification = new SPWebConfigModification(GetModificationName(), XPathLocation);
    modification.Owner = ChangesOwner;
    modification.Sequence = 0;
    modification.Type = ModificationType;
    modification.Value = GetModificationValue();

The other method is RemoveFromWebApplication which does the reverse of AddToWebApplication and removes the modification from the list of modifications for a particular web application.

public void RemoveFromWebApplication(SPWebApplication target)
    Collection <SPWebConfigModification> mods = target.WebConfigModifications;
    // Find this modification if originally created by the owner and remove it
    SPWebConfigModification mod;
    int i = 0;
    string modName = GetModificationName();
    while (i < mods.Count)
        mod = mods[i];
        // Is this the modifcation we want to remove?
        if (mod.Owner.Equals(ChangesOwner) && mod.Name.Equals(modName))

I followed Vince Rothwell’s post in removing the modification.  In some books and blogs you’ll see examples where they create a new SPWebModification object with the same name as one that’s already in the WebConfigModifications collection, and then call the Remove method on the WebConfigModifications collection.  In my method above I search through the WebConfigModifications collection to get a reference to the original modification that was added (via a call to AddToWebApplication), and then remove that item from the collection.

That’s it for the ConfigModification class.  If you can remember back to the beginning, the object of this exercise is to implement changes to the web.config to allow the use of a proxy server and in the next post I’ll look at how to actually create a feature to do this.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at

%d bloggers like this: