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;
                }
            }
        }
    }

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="HelveticSolutions.Web.Sitecore.Pipelines.QueryableDatasourceProcessor, HelveticSolutions.Web"/>
      </mvc.renderRendering>
    </pipelines>
  </sitecore>
</configuration>

Content Editor

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

DatasourceQuery

Enable dynamic MVC content place holders for Sitecore – Part 2

This is the second part of a blog series talking about enabling dynamic content place holders with meaningful names using MVC.

Let’s see how we can bring the page editor to life with what was described in the first part of this series. As a starting point I got inspired by an article on Stackoverflow.

Customised GetAllowedRenderings

This customisation is required so that the we only need one entry in the Placeholder Settings section. Out-of-the box Sitecore is loading these settings based on the place holder name but since in our case that name is dynamic you would have to add an entry for each dynamic place holder which would be very cumbersome.
Continue reading “Enable dynamic MVC content place holders for Sitecore – Part 2”

Enable dynamic MVC content place holders for Sitecore – Part 1

Background

In one of my projects I have the requirement to allow the content administrator to create content using a large set of pre-defined content blocks (renderings). The provided design required the responsive website to wrap content rows into div tags. For this project we had to allow the content administrator to mix and match content blocks for 1 colomn, 2 column and 3 column layouts on the same page. Here’s an example of how a sample page could look like:

CompositePage

This is a simplified view of the various different content blocks. Also the number of required rows and columns vastly wary from page to page.

Since the numbers of rows would not be known at the time of creating the Sitecore layout view, only the ‘Header’, ‘Menu’ and ‘Main’ place holders could be defined in the mark-up. Each of the content rows rendered in the main content place holder would be wrapped in a row rendering that contains a place holder used to render content blocks.

In Sitecore it does not make sense to declare a content place holder with the same name more than once on a particular page. If you were trying to do that, Sitecore would render everything in the first occurrence of the place holder and ignore the others.

In order to overcome this limitation I realised that I had to find a way to declare content place holders on the fly from the content authoring environment. The following stackoverflow article describes how to do that but I didn’t like that it uses a Guid as the dynamic part of the name. Hence I decided to blog how I overcame that limitation allowing to declare more meaningful dynamic place holder names.
Continue reading “Enable dynamic MVC content place holders for Sitecore – Part 1”