Skip to content

Instantly share code, notes, and snippets.

@JamesSkemp
Last active June 22, 2017 08:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesSkemp/96b11eac7ab3696fc16c to your computer and use it in GitHub Desktop.
Save JamesSkemp/96b11eac7ab3696fc16c to your computer and use it in GitHub Desktop.
Steps to create a new Sitecore ribbon button

Add a New Command Button to a Sitecore Ribbon

The following covers how to add a new button to the Sitecore ribbon.

The following Sitecore changes must be done in the core database.

Create a new Ribbon Chunk

/sitecore/content/Applications/Content Editor/Ribbons/Chunks/*group name*
  • Template: /sitecore/templates/System/Ribbon/Chunk

  • Populate Header and ID fields.

    /sitecore/content/Applications/Content Editor/Ribbons/Chunks/group name/button name

  • Template: /sitecore/templates/System/Ribbon/Large Button

  • Populate Header, Icon, Click, and Tooltip fields.

If needed, create a new Strip (tab)

/sitecore/content/Applications/Content Editor/Ribbons/Strips/*tab name*
  • Template: /sitecore/templates/System/Ribbon/Strip

  • Populate Header, ID, and Access Key fields.

    /sitecore/content/Applications/Content Editor/Ribbons/Strips/tab name/section name

  • Template: /sitecore/templates/System/Reference

  • Populate Reference field (point to Chunk, such as sitecore/content/Applications/Content Editor/Ribbons/Chunks/*group name*)

Add to Ribbon

/sitecore/content/Applications/Content Editor/Ribbons/Ribbons/Default/*tab name*
  • Template: /sitecore/templates/System/Reference
  • Populate Reference field (point to Strip, such as sitecore/content/Applications/Content Editor/Ribbons/Strips/*tab name*)

Create a Config

  • Should be placed within App_Config\Include inside the Sitecore CMS project.
  • Example config:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:x="http://www.sitecore.net/xmlconfig/">
	<sitecore>
		<settings>
			<!-- Domain to load when previewing pages. Include the protocol but not the trailing slash. -->
			<setting name="Wsob.SitecoreCms.Commands.ViewOnPreviewCommand.PreviewDomain" value="http://example.com" />
		</settings>
		<commands>
			<command name="wsb:viewonpreview" type="Wsob.SitecoreCms.Commands.ViewOnPreviewCommand, Wsob.SitecoreCms" />
		</commands>
	</sitecore>
</configuration>

Write the Actual Code

using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Shell.Framework.Commands;
using System;

namespace Wsob.SitecoreCms.Commands
{
	internal class ViewOnPreviewCommand : Command
	{
		public override void Execute(CommandContext context)
		{
			if (context != null && context.Items.Length == 1)
			{
				Item contextItem = context.Items[0];

				if (contextItem.Visualization.Layout != null)
				{
					openNewWindow(contextItem);
				}
				else
				{
					Log.Info("[WSB Preview] Failed previewing item " + contextItem.ID, this);
					Sitecore.Context.ClientPage.ClientResponse.Alert("Currently only pages can be previewed. Please select a page with layout.");
				}
			}
		}

		private void openNewWindow(Item item)
		{
			var urlOptions = new Sitecore.Links.UrlOptions();
			urlOptions.LanguageEmbedding = Sitecore.Links.LanguageEmbedding.Never;
			urlOptions.LowercaseUrls = true;

			var previewDomain = Sitecore.Configuration.Settings.GetSetting("Wsob.SitecoreCms.Commands.ViewOnPreviewCommand.PreviewDomain");
			if (string.IsNullOrWhiteSpace(previewDomain))
			{
				previewDomain = "http://example.com";
			}

			var currentSite = Sitecore.Context.GetSiteName();
			Sitecore.Context.SetActiveSite("bus");
			var itemUrl = Sitecore.Links.LinkManager.GetItemUrl(item, urlOptions);
			Sitecore.Context.SetActiveSite(currentSite);

			Log.Info("[WSB Preview] Item url: " + itemUrl, this);

			itemUrl = previewDomain + itemUrl;

			Sitecore.Context.ClientPage.ClientResponse.Eval(string.Format("window.open('{0}');", itemUrl));
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment