Contextual Ribbons in Page Editor

On my blog post on Contextual Ribbons I got a comment by Jason Cox. The question was whether or not we can have contextual ribbons in the Page Editor.

Turns out, you can – and it’s a lot easier than it looks at first.

In webroot\sitecore\shell\Applications\WebEdit\WebEditRibbon.aspx there’s a codebeside file that’s being used to render the WebEditRibbonForm:

<sc:CodeBeside runat="server Type="Sitecore.Shell.Applications.WebEdit.WebEditRibbonForm, Sitecore.Client" />

If we change this to our own class, it’s very easy to get the contextual tabs. So, I commented out the existing codebeside, and replaced it with:

<sc:CodeBeside runat="server" Type="TestApplication.ContextualWebEdit.CustomWebEditRibbonForm, TestApplication" />

This class then needs to inherit from the WebEditRibbonForm. The only thing we have to do is override the RenderRibbon method. I copied the code straight from Reflector, and changed the line highlighted below.

public class CustomWebEditRibbonForm : WebEditRibbonForm
{
  protected override void RenderRibbon(Item item)
  {
    string str2;
    SiteRequest request;
    Assert.ArgumentNotNull(item, "item");
    string queryString = WebUtil.GetQueryString("mode");
    Ribbon ribbon2 = new Ribbon();
    ribbon2.ID = "Ribbon";
    ribbon2.ShowContextualTabs = true;
    ribbon2.ActiveStrip = (queryString == "preview") ? "VersionStrip" : WebUtil.GetCookieValue("sitecore_webedit"activestrip");
    Ribbon ctl = ribbon2;
    string str3 = queryString;
    if (str3 != null)
    {
      if (!(str3 == "preview"))
      {
        if (str3 == "edit")
        {
          str2 = this.IsSimpleUser() ? "/sitecore/content/Applications/WebEdit/Ribbons/Simple" : "/sitecore/content/Applications/WebEdit/Ribbons/WebEdit";
          goto Label_00A2;
        }
      }
      else
      {
        str2 = "/sitecore/content/Applications/WebEdit/Ribbons/Preview";
        goto Label_00A2;
      }
    }
    str2 = "/sitecore/content/Applications/WebEdit/Ribbons/Preview";
    Label_002:
    request = Context.Request;
    Assert.IsNotNull(request, "Site request not found.");
    CommandContext context = new CommandContext(item);
    context.Parameters["sc_pagesite"] = request.QueryString["sc_pagesite"];
    ctl.CommandContext = context;
    context.RibbonSourceUri = new ItemUri(str2, Context.Database);
  }
}

As displayed above, one change is all we need: ribbon2.ShowContextualTabs needs to be set to true. I’ve left the rest of the code exactly the way it was according to Reflector.

As a little test I’ve set the Ribbon field of an item to sitecore/content/Applications/Content Editor/Ribbons/Contextual Ribbons/Images, so I’d expect to see the Media section and its buttons appear. This should probably be set in the Standard Values instead of on a regular item, but you get the gist.

The result:

contexteditor

It works quite nice, actually.

Advertisements

One thought on “Contextual Ribbons in Page Editor

  1. Pingback: Contextual Ribbons In Page Editor – Part 2 | Sitecore and such

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s