Today I ran into a blog post from Tony Bierman, talking about one of the new surprises of WSS3: SPWebConfigModification object. This object is an interface for changeing the web.config file(s) on all SharePoint servers in the server farm. So, in case you have missed it:
"At some point in your career as a SharePoint developer you may find it necessary to push out a change to a SharePoint web application's web.config file for all servers in the farm. In the simplest of scenarios, it may be realistic to make the change to web.config manually, but if you are creating a packaged application for redistribution or there are many servers in the SharePoint farm, a better approach might be to make the web.config modifications programmatically. The WSS v3 API contains the SPWebConfigModification object which allows you to do just that."
using System;
using System.Globalization;
using Microsoft.SharePoint.Administration;
public class Program
{
static void Main(string[] args)
{
AddAjaxHandlerToWebConfig("Default Web Site");
}
/// <summary>
/// Adds the Ajax HttpHandler to a web application's web.config for all servers in the farm
/// </summary>
/// <param name="webAppName">Name of web application</param>
private static void AddAjaxHandlerToWebConfig(string webAppName)
{
string assmDetails =
string.Format(CultureInfo.InvariantCulture,
"Microsoft.Web.Handlers.ScriptResourceHandler, Microsoft.Web.Extensions, Version={0}, Culture=neutral, PublicKeyToken={1}",
new object[] { "1.0.61025.0", "31bf3856ad364e35" });
SPWebConfigModification modification =
new SPWebConfigModification("add[@name='AjaxHttpHandler']", "configuration/system.web/httpHandlers");
modification.Owner = "Ajax";
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
modification.Value =
string.Format(CultureInfo.InvariantCulture,
"<add verb=\"{0}\" path=\"{1}\" type=\"{2}\" validate=\"{3}\"/>",
new object[] { "GET", "ScriptResource.axd", assmDetails, "false" });
SPWebApplication webApp = SPWebService.ContentService.WebApplications[webAppName];
if (webApp != null)
{
webApp.WebConfigModifications.Add(modification);
SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}
}
Read the complete article