Lars Nielsen's Discoveries

February 6, 2010

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

Filed under: Development,SharePoint — Lars Nielsen @ 7:09 pm
Tags: ,

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

The ProxyConfigModifcation is one a set of classes – here’s a nice diagram of the whole set:

Class Diagram

The ProxyConfigModification is pretty simple.  It has three properties, and each property is stored as an element in an array of 3 AttributeConfigModification objects.  I’ll deal with this class later, but in essence it represents a single attribute in the XML element.  There are 3 new attributes to add to the proxy element, so there are 3 AttributeConfigModification objects in an array.


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; }
}
/// <summary>
/// IP or DNS address of proxy server (including port number)
        /// </summary>
public string ProxyAddress
{
get { return AttributeModifications[1].AttributeValue; }
set { AttributeModifications[1].AttributeValue = value; }
}
/// <summary>
/// bypassonlocal attribute - should be either "True" or "False"
/// </summary>
public string BypassOnLocal
{
get { return AttributeModifications[2].AttributeValue; }
set { AttributeModifications[2].AttributeValue = value; }
}

All the properties are string type for simplicity although two of them (UsesSystemDefault and BypassOnLocal) ought really to be booleans.  You could give them a boolean type to improve the type checking.

The class has a constructor and 2 other methods:

public ProxyConfigModification(string owner)
{
_attributeModifications = new AttributeConfigModification[3];
AttributeModifications[0] = new AttributeConfigModification(owner, @"configuration/system.net/defaultProxy/proxy");
AttributeModifications[1] = new AttributeConfigModification(owner, @"configuration/system.net/defaultProxy/proxy");
AttributeModifications[2] = new AttributeConfigModification(owner, @"configuration/system.net/defaultProxy/proxy");
AttributeModifications[0].AttributeName = "usesystemdefault";
AttributeModifications[1].AttributeName = "proxyaddress";
AttributeModifications[2].AttributeName = "bypassonlocal";
}

/// <summary>
/// Add modification to the target web application
/// </summary>
/// <param name="target">Target web application</param>
public void AddToWebApplication(SPWebApplication target)
{
foreach (AttributeConfigModification mod in AttributeModifications)
{
mod.AddToWebApplication(target);
}
}
/// <summary>
/// Remove modification from the target web application
/// </summary>
/// <param name="target">Target web application</param>
public void RemoveFromWebApplication(SPWebApplication target)
{
foreach (AttributeConfigModification mod in AttributeModifications)
{
mod.RemoveFromWebApplication(target);
}
}

Looking at this you can see how much it is making heavy use of the AttributeConfigModification class.  I’ll explain that class more in the next post, so don’t worry about the details for now.  The constructor initialises the array of 3 AttributeConfigModification objects, each of which has a name and they all share the same XPath location within the web.config file (“configuration/system.net/defaultProxy/proxy”).

Apart from the constructor there are simply two methods to add or remove the proxy modification from the web.config.  They each take a web application as a target.  When you call these methods they don’t actually update the web.config immediately, they just queue up the changes in a list of modifications that form part of every SPWebApplication (see part 1 for more details).  The changes will actually be written out to the web.config file later on in the feature activation code.

You can see that this class wraps up all the information and functionality you need to configure the proxy settings in 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

Create a free website or blog at WordPress.com.

%d bloggers like this: