Custom data source for rendering elements

I have a Sitecore 8 MVC solution, and I need to extend the behavior of the data source. This is very similar to what other users used to do with the requested data sources (for example, http://www.cognifide.com/blogs/sitecore/reduce-multisite-chaos-with-sitecore-queries/ , etc.) But I connected to <mvc.getXmlBasedLayoutDefinition> . It works fine, and my custom data sources are allowed, as they are entered in the layout field for an element or standard values.

But, when the user data source is specified as the default data source for the render item, things get a little more complicated. I could solve it on the same pipeline, but this solution did not look very beautiful. This means that I would have to load every render that does not have the data source specified in the layout, and do the processing and allow it from there. This should be a more natural way.

Does anyone know where to put such implementation logic for the default data source? (The <resolveRenderingDatasource> looked promising, but didn't run in this scenario)

+4
source share
1 answer

From what I understand, you can extend the XmlBasedRenderingParser class. Here are the steps that should do the trick:

  • Create a new App_Config\include\Sitecore.Mvc.Custom.config file:
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <initialize> <processor patch:after="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" type="My.Assembly.Namespace.RegisterCustomXmlBasedRenderingParser, My.Assembly"/> </initialize> </pipelines> </sitecore> </configuration> 
  1. Create a CustomXmlBasedRenderingParser class:
 using Sitecore; using Sitecore.Data.Items; using Sitecore.Mvc.Extensions; using Sitecore.Mvc.Presentation; namespace My.Assembly.Namespace { public class CustomXmlBasedRenderingParser : XmlBasedRenderingParser { protected override void AddRenderingItemProperties(Rendering rendering) { RenderingItem renderingItem = rendering.RenderingItem; if (renderingItem != null && !rendering.DataSource.ContainsText()) { rendering.DataSource = ResolveRenderingItemDataSource(renderingItem); } base.AddRenderingItemProperties(rendering); } private static string ResolveRenderingItemDataSource(RenderingItem renderingItem) { string dataSource = string.Empty; if (renderingItem.DataSource != null && renderingItem.DataSource.StartsWith("query:")) { string query = renderingItem.DataSource.Substring("query:".Length); Item contextItem = Context.Item; Item queryItem = contextItem.Axes.SelectSingleItem(query); if (queryItem != null) { dataSource = queryItem.Paths.FullPath; } } return dataSource; } } } 
  1. Create a RegisterCustomXmlBasedRenderingParser class:
 using Sitecore.Mvc.Configuration; using Sitecore.Mvc.Presentation; using Sitecore.Pipelines; namespace My.Assembly.Namespace { public class RegisterCustomXmlBasedRenderingParser { public virtual void Process(PipelineArgs args) { MvcSettings.RegisterObject<XmlBasedRenderingParser>(() => new CustomXmlBasedRenderingParser()); } } } 

Moreover, if you want your code to run for a DataSource defined for both Rendering and presentation, you should use the following code:

 using System.Xml.Linq; using Sitecore; using Sitecore.Data.Items; using Sitecore.Mvc.Presentation; namespace My.Assembly.Namespace { public class CustomXmlBasedRenderingParser : XmlBasedRenderingParser { public override Rendering Parse(XElement node, bool parseChildNodes) { Rendering rendering = base.Parse(node, parseChildNodes); ResolveRenderingItemDataSource(rendering); return rendering; } private static void ResolveRenderingItemDataSource(Rendering rendering) { if (rendering.DataSource != null && rendering.DataSource.StartsWith("query:")) { string query = rendering.DataSource.Substring("query:".Length); Item contextItem = Context.Item; Item queryItem = contextItem.Axes.SelectSingleItem(query); if (queryItem != null) { rendering.DataSource = queryItem.Paths.FullPath; } } } } } 

Remember that this code is not verified correctly and may not work out of the box in your environment. In any case, I hope this gives you at least a good indication of where to start.

+2
source

All Articles