I recently decided to build a little solution to more easily switch on debugging on a web application in SharePoint. Currently there are four different places to switch on in web.config for debug info. These are the call stack setting, the debug compilation settings and the custom errors setting.
SharePoint luckily provides an API we can use to make modifications to the web.config file and the great thing about this API is it takes care of a multiple web front end scenario for us, as long as we make the changes through this API then they are propagated to all web front ends belonging to that web application.
The API classes in question are SPWebConfigModification, a bunch of which you build up and add to the WebConfigModifications collection of an SPWebApplication object representing your SharePoint web app. We get hold of the SPWebApplication object through an SPWebService class and passing the current HTTP context.
e.g.
...
SPWebService webService;
webService = SPFarm.Local.Servers.GetValue();
SPWebApplication app = webService.WebApplications[SPControl.
GetContextWebApplication(HttpContext.Current).Id];
SPWebConfigModification mod = new SPWebConfigModification();
mod.Path = "configuration/SharePoint/SafeMode";
mod.Name = "CallStack";
mod.Owner = "PlanetWilson.SharePoint.SPDebugSwitch";
mod.Value = enable.ToString();
mod.Sequence = 0;
mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
app.WebConfigModifications.Add(mod);
You can see in the full source code that we get a hold of the web application object using an SPWebService object, add the four sets of changes then call the ApplyWebConfigModifications method on the web service - that's it we are done!
It is a fairly simple project. The rest of the files deal with deployment as a solution which is scoped at the "site" level, i.e. site collection and requires site collection admin rights for access to the functionality. A layouts application is used to execute this code and links from site settings take the user to the layouts application.
Once the solution has been installed, deployed and activated on a site collection you will find two extra links under the "Site Collection Administration" section of site settings, one to enable and one to disable debugging support.
The source code and solution file can be found here on CodePlex.
0 comments:
Post a Comment