Lars Nielsen's Discoveries

April 14, 2010

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

Filed under: Development,SharePoint — Lars Nielsen @ 8:16 pm
Tags: ,

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

In this post I’ll describe the final class in the original set of classes in this diagram:

Class Diagram

The ProxyConfigModification class represents a specific change to the web.config that sets up the proxy server.  There’s not much to this class other than the properties that it needs to know (BypassOnLocal, ProxyAddress and UseSystemDefault) which become the attributes in the line of XML that will go into the web.config.  Each of these is actually an item in the AttributeModifications list which is a list of AttributeModification objects.  So the property declarations look like this:

private AttributeConfigModification[] _attributeModifications;
private AttributeConfigModification[] AttributeModifications
{
      get
      {
           return _attributeModifications;
      }
}

/// <summary>
/// usesystemdefault attribute - should be either "True" or "False"
/// </summary>
public string UseSystemDefault
{
     get
     {
          return AttributeModifications[0].AttributeValue;
     }
     set
     {
          AttributeModifications[0].AttributeValue = value;
     }
}

You can see how the UseSystemDefault property just maps onto item 0 of AttributeModifications. The other two properties map, logically enough, to items 1 and 2.

The constructor method creates the 3 items in the  _attributeModifications, setting the XPath location and the names of each attribute as appropriate.

The two other methods, AddToWebApplication and RemoveFromWebApplication, simply iterate through the AttributeModifications and call the method on each item, like this:

public void AddToWebApplication(SPWebApplication target)
{
      foreach (AttributeConfigModification mod in AttributeModifications)
      {
           mod.AddToWebApplication(target);
      }
}

That’s it. You might wonder why I created a list of AttributeModification objects rather than using an ElementModification object.  After all, what we want to do is to add a new XML element to the web.config, so why not use ElementModification?  The reason is that the proxy element is already there in the web.config in SharePoint, so in fact we want to change the attributes rather than add a new element.

In the next post I’ll look at how we can wrap all this up into a feature so that end users (well, a SharePoint administrator more likely) can just click a button to add or remove the proxy entry from the web.config file.

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: