A RequiresDataSource filter for Sitecore Controller Renderings
A common requirement when creating components in Sitecore is for the component to rely on data being supplied via the DataSource. Whilst you can try your best to ensure that a DataSource is associated with a component, there will inevitably be occasions when that DataSource gets deleted or moved and ultimately isn’t provided to the component at the time of rendering.
What can work well in these occasions is to provide the content editor with some feedback that the component won’t function correctly, so that they know they shouldn’t publish the page in its current state.
In Sitecore Controller Renderings, this could be done through some code at the top of each rendering action, checking for a DataSource and acting accordingly if one wasn’t found. However, rather than write repetitve code in each rendering, a better method is to make use of a reusable ActionFilter.
When the ActionFilter is executed, it attempts to locate a DataSource within the current RenderingContext. If no DataSource is found, then a PartialView is returned and the action will not continue to execute.
With this attribute created, you can assign it to an action:
You can also assign it to a controller if you want it to execute for every action within that controller.
If you don’t specify a view, the default will be used. However, if you need to specify a particular view for a given rendering, you can:
What do you put in the view? That is completly up to what is necessary in your solution - however a good strategy is to provide a simple error message to a user using the PageEditor, but actually render nothing if the component still makes it to the published page without a DataSource:
Now, if the component is viewed within the Page Editor, and no DataSource has been set, the user will see an informative message:
As well as provided the user with some feedback, it has removed the need for the null-checking that would have otherwise been required in the action to prevent exceptions, we’re now guaranteed that the code will only be executed if a DataSource has been set.
To provide the user with some more specific feedback, and also to ensure that our code only executes if the correct type of DataSource has been applied, we can extend the attribute to create another.
RequiresDataSourceOfTemplate attribute shown below adds an additional check that occurs if the DataSource has been set, whereupon it additionally confirms that the DataSource is of the right template:
This time, as well as returning an alternate view, some additional metadata is pushed into the ViewData. That allows us to provde create a view that provides the user with a bit of information about what went wrong:
Once you have these attributes, you can decorate all of your actions that rely on DataSources with them. This will eliminate a lot of common code needing to be rewritten across these actions, so instead your actions can focus on just delivering the right content with the correct DataSources in place.