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; } } } }
Configuration
Following best practises we create a patch file for the configuration change:
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <mvc.renderRendering> <processor patch:before="*[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']" type="Swissworx.Web.Sitecore.Pipelines.QueryableDatasourceProcessor, Swissworx.Web"/> </mvc.renderRendering> </pipelines> </sitecore> </configuration>
Content Editor
Now we can use Sitecore queries in the Data Source field: