Change your IIS Web App in Dev Mode using SPWebConfigModification

Developers can, when they are in need to see the full ASP.NET error within the SharePoint pages,  turn on the StackTrace and the turn off the CustomErrors within the web.config of the IIS Web Application they are using to test out the work.

It is possible to programmatically do this by working with the SPWebConfigModification class within the Microsoft.SharePoint.Administration namespace. This is a very useful class if you want to dynamically (that is within code - e.g. within your Feature event receivers or custom installation applications). You have to option to add/modify/delete sections, child elements and attributes. To turn the web.config in dev mode and back you'll work with the following code:

       private void ToggleDevMode(string url, bool flag)         {             SPSite sitecol = new SPSite(url);             SPWebApplication iiswebapp = sitecol.WebApplication;              SPWebConfigModification webcnfg1 = new SPWebConfigModification();             webcnfg1.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;             webcnfg1.Path = "configuration/SharePoint/SafeMode";             webcnfg1.Name = "CallStack";             if (flag)                webcnfg1.Value = "True";             else                webcnfg1.Value = "False";              SPWebConfigModification webcnfg2 = new SPWebConfigModification();             webcnfg2.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;             webcnfg2.Path = "configuration/system.web/customErrors";             webcnfg2.Name = "mode";             if (flag)                webcnfg2.Value = "Off";             else                webcnfg2.Value = "On";              iiswebapp.WebConfigModifications.Add(webcnfg1);             iiswebapp.WebConfigModifications.Add(webcnfg2);              iiswebapp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();              sitecol.Dispose();         }