Setting display name from the Sitecore page editor | Rickard Andersson

Setting display name from the Sitecore page editor

In order to allow for SEO friendly URL:s in multi-language sites, the URL to an item needs to be different depending on the language that it is displayed in. For example, an article about Christmas presents might have the URL /articles/christmas-presents for the English version and /artiklar/julklappar for the Swedish version. Even though the URL:s are different, they point to the same item in the Sitecore content tree.

Sitecore supports this behavior via the display name field. If a display name is set for the current context language, Sitecore will use that name to construct the URL and if no display name is set, it will use the item name. The item name is shared between languages whereas display name is not. This works out of the box.

However, in order to allow authors to edit the display name directly in page editor, a few tweaks and a small workaround is needed, at least in the current release of Sitecore (6.5.0 update 5). We need to add an edit frame somewhere on the page and configure it to include display name as an editable field. Additionally, we need to override a handler method called by the page editor ribbon form.

Adding an edit frame is pretty straight-forward. In content editor, switch to the core database, and navigate to /sitecore/content/Applications/WebEdit/Edit Frame Buttons. Duplicate the default folder and give it a meaningful name. Then open up the folder, delete the items Insert and Edit the related item. Then edit the item called Edit and under Fields, enter __Display name.

The second step is to add the sc:EditFrame to our layout or sublayout. In our case, we wrapped the website logo in an edit frame so that when an author clicks the logo in page editor, the edit frame pops up. The final markup could look something like this:

<sc:editFrame runat="server" ID="efPage" Buttons="/sitecore/content/Applications/WebEdit/Edit Frame Buttons/Page">
    <!-- image tag here -->

Then set the DataSource for the edit frame to the current context item (e.g. Sitecore.Context.Item), either in the markup or from codebehind.

Now if you open up the page in page editor and click the logo, a Sitecore control popup should appear containing a button. If you click the button, the edit frame appears and you can edit the display name.

All is not well though. In changing display name, we are changing the URL to the page and unfortunately, page editor doesn’t pick this up when it saves the page. If you change display name and save the page, page editor will attempt to redirect you to the old URL instead of the new and you end up with a 404. The solution to this problem is a bit of a hack. We need to add our own implementation of the method SavedNotificationHandler in the page editor ribbon form. Here’s our implementation.

protected override void SavedNotification(Object sender, ItemSavedEventArgs args)
    Assert.ArgumentNotNull(sender, "sender");
    Assert.ArgumentNotNull(args, "args");

    var item = args.Item.Database.GetItem(new DataUri(new ItemUri(this.ContextUri)));

    if (item.ID == args.Item.ID)
        UrlString webSiteUrl = SiteContext.GetWebSiteUrl();

        webSiteUrl.Add("sc_mode", "edit");
        webSiteUrl.Add("sc_itemid", args.Item.ID.ToString());
        webSiteUrl.Add("sc_lang", args.Item.Language.ToString());

        SheerResponse.Eval("scNavigate(\"" + webSiteUrl.ToString() + "\", 1)");
        base.SavedNotification(sender, args);

Just put this method in a class somewhere. Now we need to edit the file /sitecore/shell/Applications/WebEdit/WebEditRibbon.aspx and replace the existing CodeBeside directive with your own. For example:

<sc:codebeside runat="server" Type="MySolution.Utils.Processor.SavedNotificationHandler, MySolution.Utils"></sc:codebeside>

What this means is that Sitecore will invoke our implementation of the SavedNotificationHandler instead of Sitecores native method. Be careful when updating Sitecore so that your change doesn’t get overwritten in the update process. It would be preferable to be able to override this method in less intrusive ways, but currently, this is the only way to solve this problem.

That’s it. You can now edit the display name of items directly from page editor.

If you want to enforce a particular naming standard for URL:s, you can add validation to the display name field, but that’s a topic for a different blog post.

Post a Comment

Comments are moderated. Your email is never published nor shared.