Web.config modification

In our project we needed to include Telerik into our SharePoint application. Telerik is used for creating high level web controls.

The vendor supplies a nice howto for this purpose.

The problem however is how to programmatically change the web.config for this.

The problem of changing the web.config can be solved by either

  • providing a new default web.config in the 12\CONFIG folder (is used when creating a new web application);
  • changing the web.config manually (not recommended for production environments!!);
  • using the API methods provided by SharePoint, as explained here

The big question now is how to implement this sequence correctly (click image to view original size):

webconfig1

When you just implement the usual way one after the other using the SPWebConfigModification object, you will see that the collection of web modifications (SPWebApplication.WebConfigModifications) is sorted primarily by the name of the modification, rather than the sequence number. Only if the name of the nodes is the same the sequence number is used. This is of course especially critical when adding nodes “add” and “remove” under a common root node (like configuration/system.web/httpHandlers) as “add” always comes before “remove”. The remove is appended at the end of the child list of the configuration/system.web/httpHandlers node.

This is not desirable as the “remove”-node removes all active server module mappings, so when a module is supposed to be executed the server does not know which handler to use. The effect can be seen for the “SharePoint Excel Export” for lists. It simply doesn’t work anymore (authorization request loop, or empty spread sheet eventhough the exported list is not empty) because the module reference fails.

It does not help to change the sequence number of the SPWebConfigModification objects and it does not help to change your code so that the insertion order of the changes is the way you would like the changes to appear in the web.config. The modifications are stored in the content database of SharePoint and when retrieved the sort order is determined neither by the sequence number (only as second parameter) nor the insertion ID. You would expect that this doesn’t matter as an xml document usually doesn’t concern itself with ordering. In this special case as the actions for HttpHandlers are executed in order this is the case though.

The solution consists of two steps (the solution for this problem is easy to remedy but it has a side effect you need to correct as well):

SPWebConfigModification ModScriptResource 
= new SPWebConfigModification(name, xpath)
            {
                Owner = OwnerModif,
                Sequence = sequenceNo,
                Type = SPWebConfigModification.
                  SPWebConfigModificationType.EnsureChildNode,
                Value = scriptResource
            };

The important variables are: name, xpath and scriptResource. The name is important for sort order and represents the path to the node relative to the variable xpath. XPath represents the absolute path of the node. ScriptResource is what is inserted in the web.config at the spot that is defined by the variable xpath.

For this scenario the sequence number is irrelevant.

webconfigmods

For the first “add” modification check out that it contains the “remove” node as well. This way you can ensure that it is always inserted in this order. This has one drawback though. When the modification should ever be removed the “remove” node stays in the web.config as the name of the webmodification is responsible for what is removed from the web.config. So this is not a clean approach. For this you need the empty dummy-entry for the “remove” node abve the “add” modification. This way both you ensure the two nodes are inserted as well as removed at the same time if you iterate over the modifications and remove all the entries for your owner module.

Advertisements

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

%d bloggers like this: