Tuesday, 26 October 2010

Updating the web.config

Problem: Updating the web.config by hand means you need to implement the change on each web servers web.config, so in a load balanced environment you need to do the change several times which leaves is open to mistakes and in the event of a rebuild or adding a new server, each web.config needs to be updated.

Hypothesis: You can either use a declarative approach or a programmatic approach.  Approaches on MSDN

Declarative approach is show on MSDN as the supplemental .config file You can implement the change using the ststadm cmd copyappbincontent or use a feature receiver
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var webApp = (SPWebApplication)properties.Feature.Parent;
webApp.WebService.ApplyApplicationContentToLocalServer();
}
Note: The feature must be scoped at "Farm" or "WebApplication" level otherwise you will get a Security exception, which makes sense.  RunWithElavatedPrivileges won't stop the error.

Resolution:  The declarative approach adds whatever is in the supplemenal config to the web.config, it does not check if it's already there so it is an option for a 1 off hidden feature however a better approach is to use an xml file that has a feature receiver that activates and deactivates the changes for maximum control.

More Info:
Additional information on using the declaritive approach.
Joel Jeffery's blog has a post on modifying your web.config programatically
Update 13 Dec 2010 - http://www.spdavid.com/category/SharePoint-2010.aspx

3 comments:

Deanna said...

Have you had any luck using this approach to add additional site map providers? I can use it for safe controls but I can't figure out the correct path to use for site map providers.

Paul Beck said...

@Deanna, Using a supplemetal config (declaritive approach to change your web.config) should work, it will add it to the provider elelment. If you can get other entries in your web.config. It really should work. Is the problem that the web.config is not updated on after deployment?

It is more stable for your farm to progratically change the web.config so you changes can be rolled in and out using feature activation/deactivation.
http://msdn.microsoft.com/en-us/library/bb861909.aspx

Comment the web.config entry you are trying to create and I'll post the code snippet.

Paul Beck said...

Hi Deanna, have a look at this post from Joel Jeffery. http://joelblogs.co.uk/2010/10/29/spwebconfigmodification-webapplication-specific-changes-in-sharepoint-2010/

Post a Comment