Sitecore RenderingsMarkerModule

Note: This module has been created as a package and is available for download along with the complete source code at Git: aceanindita

Sitecore being component based as it is, adding start and end comments to your views can be immensely helpful while debugging issues, especially if they are being worked on someone who doesn’t know the project from Adam.

Hardcoding the start / end comments isn’t a great idea for obvious reasons like being prone to developer omission in some views, refactoring / file renaming concerns etc.

In an earlier post I had shown how we had partially resolved this by making the build up of the comment dynamic using a html helper method which used the WebPageExecutingBase.VirtualPath property from the System.Web.WebPages assembly.
You can refer the post here: Using dynamic view markers in Sitecore MVC

In this method, you would need to manually wrap each of your views within a call to the said html helper method, which would dynamically include the current view path in start and end comments around your view. While this did away with the potential issues that might come out of refactoring like changing folder structures / renaming files, it still didn’t deal with the dependency on having the developer(s) remember to add the call to this html helper in every single view.

Here’s a solution for this, we added a new processor in the mvc.rendering pipeline, before and after the call of Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer which renders the actual rendering html.

Here’s the config update made for this (patch):

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.renderRendering>
        <processor type="RenderingsMarkerModule.Pipelines.MVC.RenderingMarker.RenderMarkerStart, RenderingsMarkerModule"
            patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']" />
        <processor type="RenderingsMarkerModule.Pipelines.MVC.RenderingMarker.RenderMarkerEnd, RenderingsMarkerModule"
            patch:after="processor[@type='Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc']" />
      </mvc.renderRendering>
    </pipelines>
    <settings>
      <setting name="RenderingsMarkerModule.Enabled" value="true" />
      <setting name="RenderingsMarkerModule.ShowLayoutMarkers" value="false" />
    </settings>
  </sitecore>
</configuration>

Note we have 2 additional settings here. ‘RenderingsMarkerModule.Enabled’ lets you toggle the rendering of comments on and off – this could help you turn of this rather revealing feature on staging / production. Additionally you could toggle ‘RenderingsMarkerModule.ShowLayoutMarkers’ to show / hide comments before & after your layout. It is recommended that this remain off, since having your html response start with a html comment might not always be favorable.

Here’s the code we have in place:
RenderingsMarkerModule.Pipelines.MVC.RenderingMarker.RenderMarkerStart

using Sitecore.Configuration;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;

namespace RenderingsMarkerModule.Pipelines.MVC.RenderingMarker
{
    public class RenderMarkerStart : RenderRenderingProcessor
    {
        public override void Process(RenderRenderingArgs args)
        {
            if (Settings.GetBoolSetting("RenderingsMarkerModule.Enabled", false))
            {
                Renderer renderer = args.Rendering.Renderer;
                if (renderer == null)
                    return;

                bool isLayout = renderer is ViewRenderer &&
                                ((ViewRenderer) renderer).Rendering.RenderingType == "Layout";
                bool showLayoutMarkers = Settings.GetBoolSetting("RenderingsMarkerModule.ShowLayoutMarkers", false);

                if (isLayout && !showLayoutMarkers) return;

                args.Writer.Write("\n<!-- START: " + renderer + " -->\n");
            }
        }
    }
}

RenderingsMarkerModule.Pipelines.MVC.RenderingMarker.RenderMarkerEnd

using Sitecore.Configuration;
using Sitecore.Mvc.Pipelines.Response.RenderRendering;
using Sitecore.Mvc.Presentation;

namespace RenderingsMarkerModule.Pipelines.MVC.RenderingMarker
{
    public class RenderMarkerEnd : RenderRenderingProcessor
    {
        public override void Process(RenderRenderingArgs args)
        {
            if (Settings.GetBoolSetting("RenderingsMarkerModule.Enabled", false))
            {
                Renderer renderer = args.Rendering.Renderer;
                if (renderer == null)
                    return;

                bool isLayout = renderer is ViewRenderer &&
                                ((ViewRenderer) renderer).Rendering.RenderingType == "Layout";
                bool showLayoutMarkers = Settings.GetBoolSetting("RenderingsMarkerModule.ShowLayoutMarkers", false);

                if (isLayout && !showLayoutMarkers) return;

                args.Writer.Write("\n<!-- END: " + renderer + " -->\n");
            }
        }
    }
}

Here’s a sample of the output

This module has been created as a package and is available for download along with the complete source code at Git: aceanindita

Advertisements

Export Item Data Tool (Module) for Sitecore

Download Here

Often we come across situations where we need to export data from our Sitecore instance into common formats like excel / xml / json etc. Usually we end up writing ad hoc scripts for this based on the requirements.

This is exactly what this tool does. It allows you to write sort of a query to determine what data you’d want, and let you export this data to the common formats like Excel / XML and Json.

This tool lets you nest fields to account for reference fields too. As an example, lets look at this scenario – I have a bunch of Article items, and I need to export this data into excel.

This was the structure in place:

2015-07-05_210728

So for the above templates, we would use the following query: Headline,Article Type(Title),Necessary Products(Sku,ProductType(Title)),Skill Level(Title)

The module basically does an index search and hence also needs the comma separated template id(s), search location / language and index name to do the search. In addition to the custom fields that can be added, we could also choose to include common attributes like Item Id, Item Name etc.

Here’s another simple example with the sample output data:

2015-07-05_212326

2015-05-31_114306

A look at sitecore:

2015-07-05_212710

Sample Output:

JSON

2015-07-05_213406

XML

2015-07-05_213512

The code for this tool is available in github: https://github.com/aceanindita/sitecore-export-item-data-tool

Once installed, the tool appears in the sitecore menu as:

2015-07-05_213810

To see the steps to create a sitecore module please refer: Creating a Sitecore Module

Creating a Sitecore Module

Following are the steps to create a Sitecore Module. This work is on Sitecore 7.2. Most of this information is already available, but while creating a module, I faced a few issues, to fix which, I had to look around a bit for a solution. This is why I am providing the steps I followed. Note the raw values shown below – and ensure your field values look similar for things to work right.

Core Database Items:

/sitecore/content/Applications/Export Item Data Tool

2015-07-05_214543

Raw View:

2015-07-05_214604

For the short cut to show up:

/sitecore/content/Documents and settings/All users/Start menu/Right/Export Item Data Tool

2015-07-05_214735

Raw View:

2015-07-05_214700

Here’s the code organization:

2015-07-05_214924

So deliverables in this case, would include a dll and an aspx file in the $\sitecore modules\Web\… path.

The end result:

2015-07-05_213810

2015-07-05_215914