Using SPWebConfigModification#

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

Wednesday, December 27, 2006 7:18:14 AM UTC #     | 

 

All content © 2012, Mart Muller
On this page
This site
Calendar
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Archives
Sitemap
Disclaimer

Powered by: newtelligence dasBlog 1.9.7174.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts