Sitecore 8 adding edit meta data button in experience editor

Are you still making your content author to edit the content of a page using content editor? then stop.

For content author in Sitecore the best way for them to edit a page is to use Sitecore Experience Editor. This way they can focus on editing the content on the page with visual indication what would their changes would look like. However there are some cases where content can’t be edited on the page because they are not displayed on the page itself, Metadata information is such a case.

There are some guide already out there to set up an edit meta data button in page editor in Sitecore 7 and below, and here I would like to do the same in Sitecore 8. This is based on the guide that I’ve found on http://blog.istern.dk/2015/03/02/running-sitecore-field-editor-from-a-speak-command-in-sitecore-experience-editor/ which can be reused to create other type of functionality.

First you need to install Sitecore Rocks, because in Sitecore 8 the interface is built using SpeakUI you may find it quite different with how you usually set up a new button in Sitecore 7 and below. I’ve also found it easier to create a SpeakUI component using Sitecore Rocks, so you definitely should check it out. After you’ve installed Sitecore Rocks and setup a connection to the Sitecore website you should see the following screen.

sitecore-rocks

 

Expand the core database and go to sitecore->content->applications->webedit->ribbons->webedit

Here you can see a couple of familiar strips which is what being displayed on the experience editor

rocks-webedit

experience-editor-ribbons

We want to add a new button to edit meta data here, so go in Sitecore Rocks and under the WebEdit node follow this steps

  • Add a new Page Properties strip
    • Add a new item with the template of Strip.
    • Set the header to “Page Properties” and the ID to “PagePropertiesStrip”
    • In Sitecore Rocks select tasks and design layout then add a new rendering of type Strip
  • Add a new Hidden Content chunk
    • Under the new Page Properties strip add a new item with the template of Chunk
    • Set the header to “Hidden Content” and ID to “HiddenContentChunk”
    • In Sitecore Rocks select tasks and design layout then add a new rendering of type Chunk
  • Add a new Edit Meta data button
    • Under the new Hidden Content chunk
    • Set the header to “Edit Meta Data” and ID to “EditMetaDataButton”
    • In Sitecore Rocks select tasks and design layout then add a new rendering of type LargeButton
    • Edit the rendering properties of the LargeButton
      • Set AccessKey to Meta Keywords|Meta Description (this the meta data fields that I’ve setup in Sitecore)
      • Set the Click property to trigger:button:click
      • Set the Command to LaunchFieldEditor
      • Set the PageCodeScriptFileName to the path of the LaunchFieldEditor.js file is

rocks-editmetadatabutton-properties

What it currently looks like so far

rocks-editmetadatabutton-tree

experienceeditor-editmetadatabutton

At this stage we still need to register the command to handle the edit meta data button functionality so let’s go ahead and do that.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
 <sitecore.experienceeditor.speak.requests>
 <request name="ExperienceEditor.GenerateFieldEditorUrl" type="Website.Modules.Components.ExperienceEditor.FieldEditor.GenerateFieldEditorUrl, Website.Modules"/>
 </sitecore.experienceeditor.speak.requests>
 </sitecore>
</configuration>

The GenerateFieldEditorUrl class

public class GenerateFieldEditorUrl : PipelineProcessorRequest<ItemContext>
{
 public string GenerateUrl()
 {
 var fieldList = CreateFieldDescriptors(RequestContext.Argument);
 var fieldeditorOption = new FieldEditorOptions(fieldList);
 //Save item when ok button is pressed
 fieldeditorOption.SaveItem = true;
 return fieldeditorOption.ToUrlString().ToString();
 }
 private List<FieldDescriptor> CreateFieldDescriptors(string fields)
 {
 var fieldList = new List<FieldDescriptor>();
 var fieldString = new ListString(fields);
 foreach (string field in new ListString(fieldString))
 fieldList.Add(new FieldDescriptor(RequestContext.Item, field));
 return fieldList;
 }
 public override PipelineProcessorResponseValue ProcessRequest()
 {
 return new PipelineProcessorResponseValue
 {
 Value = GenerateUrl()
 };
 }
}

The LaunchFieldEditor.js

define(["sitecore"], function (Sitecore) {
Sitecore.Commands.LaunchFieldEditor =
 {
 canExecute: function (context) {
 //YOU COULD ADD FUNCTIONALITY HERE TO SEE IF ITEMS HAVE THE CORRECT FIELDS
 return true;
 },
 execute: function (context) {
 ///CHOOSE YOUR OPTION BELOW
 // THIS IS FOR THE ALT TEXT ON IMAGE
 //context.currentContext.argument = context.button.viewModel.$el[0].firstChild.alt;
 //THIS IS THE TOOLTIP ON LINK TAG "A"
 //context.currentContext.argument = context.button.viewModel.$el[0].title;
 // THIS IS THE ACCESS KEY
 context.currentContext.argument = context.button.viewModel.$el[0].accessKey;
 Sitecore.ExperienceEditor.PipelinesUtil.generateRequestProcessor("ExperienceEditor.GenerateFieldEditorUrl", function (response) {
 var DialogUrl = response.responseValue.value;
 var dialogFeatures = "dialogHeight: 680px;dialogWidth: 520px;";
 Sitecore.ExperienceEditor.Dialogs.showModalDialog(DialogUrl, '', dialogFeatures, null);
 }).execute(context);
 }
 };
});

After all have been setup correctly the content author can now edit the meta data button through the experience editor.

experienceeditor-editmetadatabutton-result