How To Programmatically Disable Custom Errors and Enable Debugging And Stack Trace In WSS

Thursday, March 29, 2007

If you're doing SharePoint v3 development you probably already know that you should make a couple of changes to your web.config to make your time spent debugging a little more bearable.

Conveniently, there's also a new class this time around which allows you to make changes to web.config files across your farm using the WSS API.

Putting these two together, the code to make these changes would look something like this:

   1:  SPWebApplication webApp = siteCollection.WebApplication;
   2:  SPWebConfigModification callStackModification = new SPWebConfigModification("CallStack", "configuration/SharePoint/SafeMode");
   3:  callStackModification.Value = "true";
   4:  callStackModification.Owner = typeof(Program).FullName;
   5:  callStackModification.Sequence = 0;
   6:  callStackModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
   7:  SPWebConfigModification customErrorsModification = new SPWebConfigModification("mode", "configuration/system.web/customErrors");
   8:  customErrorsModification.Value = "Off";
   9:  customErrorsModification.Owner = typeof(Program).FullName;
  10:  customErrorsModification.Sequence = 1;
  11:  customErrorsModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
  12:  SPWebConfigModification debugModification = new SPWebConfigModification("debug", "configuration/system.web/compilation");
  13:  debugModification.Value = "true";
  14:  debugModification.Owner = typeof(Program).FullName;
  15:  debugModification.Sequence = 2;
  16:  debugModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
  17:  webApp.WebConfigModifications.Add(callStackModification);
  18:  webApp.WebConfigModifications.Add(customErrorsModification);
  19:  webApp.WebConfigModifications.Add(debugModification);
  20:  webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
  21:  webApp.Update();

I use this in a console application which I call from script when provisioning a farm on my development machine, you can download the source here.

You might also want to have a look at the web.config modification manager for SharePoint. It lets you manage changes made using SPWebConfigModification via the browser, and works really well for adding and removing individual changes. It would also be a good place to start from if you wanted to create a page to simultaneously flip all these debug settings without heading for the command line.

13 Comments | SharePoint

Comments

blissful abstraction said... How to Enable JavaScript Errors in SharePoint

Tuesday, April 24, 2007

Troels said... Thanks for this nice example which shows excatly what i wanted to do.
I would how ever like to be able to enable custom errors etc again.
On other sites where these web.config modifications are shown, they simply say to call the Remove funtion instead of Add funtion to remove the changes:
webApp.WebConfigModifications.Remove(debugModification);

I cant get this to work. How do you accomplish this?

Wednesday, May 02, 2007

Paul Horsfall said... That should work, and if you download the code from the post that's what I do too.

I'd suggest checking that you use the same constructor (for your SPWebConfigModification) and set the same Owner when you're trying to remove the change as you used when you originally added it. I'd also check that you're calling webApp.Update() after making any changes.

If you still have no luck, download the source for my console app from the post and try that. Once built, you can test add and remove as follows:

wssdebug http://localhost /enable
wssdebug http://localhost /disable

Let me know how you get on.

Wednesday, May 02, 2007

Josh Unger said... Thanks... you got me on the right track for doing the same thing with appSettings. Here is the code -

SPWebConfigModification modification = new SPWebConfigModification("file", "configuration/appSettings");
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;
modification.Value = "Custom.config";

Saturday, September 01, 2007

dhfhdgfh said... gfhdhg

Friday, April 25, 2008

Premarin side effects. said... Premarin. Premarin foals. Premarin infertility study. Statistics of premarin. Stories told about premarin. Long term use of premarin cause of asthma. Side effects of premarin.

Thursday, May 08, 2008

Alprazolam. said... Alprazolam online. Alprazolam no prescription. Alprazolam. Cheap 2mg alprazolam.

Friday, May 09, 2008

Alprazolam. said... Alprazolam.

Monday, May 12, 2008

Buy cialis pharmacy online. said... Cialis fedex. Buy cialis. Cialis lawyers.

Monday, May 12, 2008

Mazda. said... Mazda.

Saturday, May 17, 2008

Soma. said... Soma. Soma online.

Saturday, May 17, 2008

Prozac mood disorder. said... Phentermine prozac. Dosage for prozac. Prozac dangers. Compare prozac and zoloft. Prozac.

Tuesday, May 20, 2008

Animal. said... Animal.

Thursday, May 29, 2008

Post a Comment

Title
 
Name (Required)
 
Email (Optional, and never displayed)
Website (Optional)
Comments
 
Please add 7 and 3 and type the answer here: