Extending Presentation Details Dialogue in Content Editor

March 15, 2017

Blog | Development | Extending Presentation Details Dialogue in Content Editor

Many developers (and the vast majority of content authors) find themselves leaning towards Sitecore’s Content Editor when performing content entry instead of the often slow Experience Editor.  Once a core understanding is reached in regards to page construction in Sitecore, Content Editor is simply faster to get work done with. On a site that is highly-modularized, which is best practice, the Presentation Layout Details can be difficult to understand at-a-glance, so I’ll show you how to extend presentation details dialogue in the Content Editor throughout this post.

In the view above (taken from Sitecore 7) for example, it is impossible to know what the Images are on the page.  It is possible to rely on placeholders for context, but even then, without selecting each individual control and editing, it is difficult to know for certain what each Image is.

Device Editor 2.0

This problem can be alleviated by enhancing the Device Editor dialogue.  Screenshot below from Sitecore 8:

The updated dialogue shows the display name of the datasource, text-aligned to the right.  On hover, the path to the datasource is displayed.

How To Implement Changes

While this enhancement requires modifications to core Sitecore components, it can be accomplished with minimal effort.  First, create a new class in your solution that is a copy of “Sitecore.Shell.Applications.Layouts.DeviceEditor.DeviceEditorForm,Sitecore.Client”.  You will need to modify the namespace to match your project and add references to eliminate errors.

Next, find the method with the following signature private void RenderRenderings(DeviceDefinition deviceDefinition, int selectedIndex, int index). At the top of this method, add the following code:

/* updates */
Item contextItem = null;
var itemId = WebUtil.GetQueryString("id");
if (!string.IsNullOrWhiteSpace(itemId))
	contextItem = Client.ContentDatabase.GetItem(new ID(itemId));
/* end updates */

This code sets the context item, which is used later in the method for retrieving relative datasources.  Next, inside the primary foreach block, find where the webControl[“Header”] is assigned the (object) obj.DisplayName (Note: this was decompiled from the original, so syntax may differ slightly.)  Replace this assignment with the following:

/* updates */

var datasourceDisplayName = "";
var datasourcePath = "";

if (!string.IsNullOrWhiteSpace(renderingDefinition.Datasource))
{
	if (ID.IsID(renderingDefinition.Datasource))
	{
		var datasourceItem = Client.ContentDatabase.GetItem(new ID(renderingDefinition.Datasource));

		if (datasourceItem != null)
		{
			datasourceDisplayName = datasourceItem.DisplayName;
			datasourcePath = datasourceItem.Paths.Path;
		}
	}
	else if(contextItem != null)
	{
		// for SXA
		var datasourceItem = Client.ContentDatabase.GetItem(contextItem.Paths.Path + renderingDefinition.Datasource.Replace("page:", "")); 

		if (datasourceItem != null)
		{
			datasourceDisplayName = datasourceItem.DisplayName;
			datasourcePath = renderingDefinition.Datasource;
		}
	}
}

if(!string.IsNullOrWhiteSpace(datasourceDisplayName))
	webControl["Header"] = (object)obj.DisplayName + "<span style=\"position: absolute; font-style: italic; font-weight: normal; right: 4px;\" title=\"" + datasourcePath + "\">" + datasourceDisplayName + "</span>";
else
	webControl["Header"] = (object) obj.DisplayName;

/* end updates */

The above code will fill in the datasource DisplayName and add the datasource Path on hover.

Lastly, to ensure our new code is executed on this dialogue, find the file “/sitecore/shell/Applications/Layouts/DeviceEditor/DeviceEditor.xml” and modify the CodeBeside control:

<CodeBeside Type="YourNamespace.YourClass,YourAssembly"/>

If at any point, you wish to revert this change, simply revert the change made to DeviceEditor.xml.

In Closing

With these enhancements, Device Editor becomes editor-friendly by seamlessly giving the editor a snapshot into the datasource name and path to help them more quickly diagnose presentation issues.  Controls with no datasource are simply blank- which can be expected in the case of a structural component, or unexpected in the case of a control that requires a datasource to function properly.

While these changes require Sitecore to query each control on the page for a datasource, we have not noticed any noticeable drop in performance.  It is certainly an enormous improvement over reviewing and remembering each control individually.

Since this is a change to core Sitecore code, a fully working code file was intentionally excluded from this post. The above technique works in all versions of Sitecore, although line numbers may differ slightly between versions.  It is worth stating that a change like this would need to be reviewed after any upgrade to ensure it does not clash or eliminate new functionality/bug fixes in the original source code.

An untested, yet highly promising use-case for this change is in Sitecore Experience Accelerator (SXA).  SXA allows editors to build out pages using a vast library of existing controls.  This enhancement provides greater clarity to a page that contains a large number of the same components.

John Rappel

Technical Lead
Tags
  • Content Management
  • Sitecore
  • Tutorial
  • Web Development
subscribe to GeekHive's newsletter

Never miss out on a Sitecore update again!

Our newsletter features a blog roundup of our top posts so you can stay up to date with industry trends, tutorials, and best practices.

Recent Work

Check out what else we've been working on