Queryable data source in Sitecore MVC

Unfortunately, Sitecore does not support queries in the Data Source field for renderings out-of-the-box but I have found a blog that describes how a pipeline processor can be created to enable queries. However, the blog post is limited to WebForms implementations so I spent a bit of time making this feature available for all MVC friends out there.

Processor code

All we need is a RenderRenderingProcessor:

public class QueryableDatasourceProcessor : RenderRenderingProcessor
        public override void Process(RenderRenderingArgs args)
            string dataSource = args.Rendering.DataSource;
            if (dataSource.StartsWith("query:"))
                string query = dataSource.Substring("query:".Length);
                Item queryItem = args.PageContext.Item.Axes.SelectSingleItem(query);
                if (queryItem != null)
                    args.Rendering.DataSource = queryItem.Paths.FullPath;


Following best practises we create a patch file for the configuration change:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
        <processor patch:before="*[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']"
            type="HelveticSolutions.Web.Sitecore.Pipelines.QueryableDatasourceProcessor, HelveticSolutions.Web"/>

Content Editor

Now we can use Sitecore queries in the Data Source field: