Description
Not sure if this has been done or not but it seems unlogical to have the web.config modifications hard coded into the Feature Receiver, so I whipped up a quick mechanism to read it from an external config XML file. Feel free to use and improve as desired.
The code uses LINQ to XML for reading the config file so .NET Framework 3.5 is a requirement on your SharePoint server. If you can't have it you'll need to modify the code to use .NET Framework 2.0 XML classes.
Feature Receiver
Doesn't contain anything rather than a call to the DeploymentUtility for handling the modifications.
WebConfigUtility
Whipped together over time after trial and error. Abstracts the SPWebConfigModification class and provides the following methods:
-
AddNodeValue(name, xpath, scriptResource): used for adding elements.
-
AddSystemSection(name, xpath): used for adding or ensuring a section. These will NOT be deleted when calling the removal method.
-
AddCustomSection(name, xpath): used for adding or ensuring a custom section that WILL BE deleted when calling the removal method.
-
AddAttribute(name, xpath, value): used for adding attributes to elements.
-
SaveWebConfig(webApp): saves the batch of modifications to the web.config file(s) for the specified Web Application.
-
RemoveWebConfigEntries(webApp): removes the modifications for the specified Web Application and current 'Owner'.
-
DeploymentUtility
Acts as intermediary layer between the Feature Receiver and the WebConfigUtility. From the Feature Definition it will read the Config XML file using LINQ and will invoke the correct method of the WebConfigUtility via reflection.
For the structure requirements of the Config XML file see "webconfig.xml" below.
feature.xml
Just your ordinary Feature.xml file. It has two required properties to specify the owner and the webconfig file name:
webconfig.xml
Contains the modifications that need to be applied. This file needs to be built using the following structure:
MethodToInvoke">
MethodSignatureParam1">
MethodSignatureParam1StringValue
MethodSignatureParam2">
MethodSignatureParam2StringValue
..
...
! The action name has to be identical to the 'Add' method you want to invoke
! The number of params must be identical to the 'Add' method number of parameters
! Put the params in the correct order as required by the method signature (the 'name' attribute is just for clarity)
Use
I have packed the sample feature receiver (can be used for activating Ajax for .NET 3.5) with all classes and files available for download. Since this will probably be part of a larger solution you'll need to incorporate those classes in your own code if desired.
- Visual Studio 2008 project
Requirements
- WSS 3.0 or MOSS 2007
- .NET Framework 3.5 (because of LINQ)
Note
Why two helper classes ? Just didn't have time to whip it up into a single class ;)
I take no responsability for anything that happens while using this code.