Enable/Disable Custom Error by editing Web.Config using PowerShell
To reveal actual errors in SharePoint, we used to turn off custom error page by editing web.config file, located in the root of SharePoint virtual directory.
- Open the web.config in Notepad
- Search for “CallStack”, Change it from “false” to “true”
- Search for “CustomErrors mode”, Change its value from “On” to “Off”
- Save and Close.
Warning: Web.config file is highly sensitive! even a small typo may result your SharePoint site Crash!!
Typical SharePoint 2007 “Unknown Error”
and SharePoint 2010’s “An unexpected error has occurred”
Alright, Wouldn’t it be a good idea to make these changes by PowerShell Scripts? Sure, Here is the PowerShell script to enable or disable SharePoint custom error page and reveal actual error by editing Web.Config file:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function ConfigureCustomError()
{
Param( [Parameter(Mandatory=$true)] [string]$WebAppURL, [parameter(Mandatory=$true)] $Option )
#Get the web application
$WebApp = Get-SPWebApplication $WebAppURL
#Get the Web Application Zone
$Zone = $WebApp.AlternateUrls[0].UrlZone
# Get the IIS settings for the zone
$IISSettings = $WebApp.IISSettings[$zone]
# Get the path from the settings
$WebConfigPath = $IISSettings.Path.ToString() + "\web.config"
#Backup web.config file
$now = [datetime]::now.ToString('dd-MM-yyyy hh-mm-ss')
$BackupFile =$IISSettings.Path.ToString()+ "\web.config.$now"
copy-item $WebConfigPath -destination $BackupFile
#Get the Web.config File
$XMLDoc = new-object System.Xml.XmlDocument
$XMLDoc.Load($WebConfigPath)
switch($option)
{
"OFF" {
#Apply Change
$XMLDoc.get_DocumentElement()."sharepoint".safemode.callstack = "True"
$XMLDoc.get_DocumentElement()."system.web".customErrors.mode = "Off"
Write-Host "Custom Error Mode has been Disabled, Now you can get the actual errors!"
}
"ON" {
$XMLDoc.get_DocumentElement()."sharepoint".safemode.callstack = "False"
$XMLDoc.get_DocumentElement()."system.web".customErrors.mode = "ON"
Write-Host "Custom Error Mode has been Enabled, SharePoint displays default custom error Page!"
}
}
$XMLDoc.Save($WebConfigPath)
}
#Call the function to Turn OFF custom errors
ConfigureCustomError "https://sharepoint.crescent.com" "OFF"