Protect Your Sitecore Renderings From Bad Datasources

We’ve all been there before, you get the urgent email off hours, or the call while you’re sleeping. “The site is down!  Our homepage is not coming up!” You go to check and sure enough, the error handler page is displaying when you hit the homepage.  Checking the logs, you see your old friend, “Object reference not set to an instance of an object.”

There are many reasons why a rendering can get published with an invalid or missing datasource.  The datasource could still be stuck in workflow, but the page it was added to was approved and published. A content editor may have set a publishing restriction on the datasource, setting up content in advance to be published at a certain date dictated by the business.  Perhaps the datasource was created in English, but the site is multilingual and no version in Spanish exists.

There are strategies to handle each of these scenarios, but content editors are always finding ways to do unexpected things.  A Sitecore developer needs to write code that can handle these situations gracefully, without bringing down the page or emitting malformed HTML.  However, checking in every rendering for null or invalid datasources can become tedious.  This is ideally handled in a base class that your renderings inherit from. Continue reading Protect Your Sitecore Renderings From Bad Datasources