Programatically using OOTB SharePoint Error Handling

Okay, so if you get an error in your custom code that you want to handle using the "out of the box" SharePoint mechanisms, use the following lines of code…  

Make sure you only call the 'TransferToErrorPage' method if you are handling errors in the a user interface (web part, page, etc…).  Don't call that method in a workflow, event handler, etc…

//This will write to SharePoint's log files.

Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Your Error Message");

//This will redirect you to SharePoint's error page.

Microsoft.SharePoint.Utilities.SPUtility.TransferToErrorPage("Message to display on the page");

 

 

Leave a Reply