Deane Barker
Sep 2, 2010
  4794
(1 votes)

Automatically Naming Pages

Here’s a fact -- not every page needs to be explicitly named.  I mean, they all have to have names, but you shouldn’t have to manually enter them all the time.

Often, a page should have its name derived from other properties.  For instance, if you have a page representing a person, you’re probably going to have separate fields for FirstName and LastName.  By logical extension then, the page name should be something like “FirstName LastName,” or “LastName, FirstName.”  You don’t want the editor to have to enter three things – the first name, the last name, and the page name…which is the first name and the last name all over again.

Now, you can set page names from code.  Just catch the PageCreating or PageSaving event, and set the value of e.Page.PageName, no problem.  But this is less than ideal because the pesky “Name” field is still in Edit Mode just begging for some input.  This looks tacky (“Yeah, you have to put something in there…but we’re not going to use it…so, it doesn’t really matter…”), and it’s potentially confusing for editors (“I entered X there, and now its set to Y…”).

So, how do you get rid of it?  I asked this question in the forums, and Linus showed me how it’s done.

The EditPanel object throws an event, which you can hook to modify the page that’s loading into it.  What you do is just change the DisplayEditUi boolean on the Pagename property.  You can do this in Application_Start, like this:

protected void Application_Start(Object sender, EventArgs e)
{
    EPiServer.UI.Edit.EditPanel.LoadedPage += new EPiServer.UI.Edit.LoadedPageEventHandler(EditPanel_LoadedPage);
}
private void EditPanel_LoadedPage(EPiServer.UI.Edit.EditPanel sender, EPiServer.UI.Edit.LoadedPageEventArgs e)
{
    e.Page.Property["Pagename"].DisplayEditUI = false;
}

This works great, but since I had 3-4 page types that needed to be handled, I built it out a little further.  I link to a class file below that you’re welcome to use.  In it, you have a List of PageTypeNames that should have their name field suppressed, like this:

private static List<string> affectedPageTypes = new List<string>() { "Council Member", "Periodical" };

Additionally, there’s a switch statement lower down where you can put the logic to derive your page names based on type:

// Modify the code below to custom-name your pages
switch (e.Page.PageTypeName)
{
    case "Council Member":
        // Example: "Barker, Deane"
        pageName = String.Format(
            "{0}, {1}",
            e.Page.Property["LastName"].ToString(),
            e.Page.Property["FirstName"].ToString());
        break;

    case "Periodical":
        // Example: "2010: Vol. 32, No. 2"
        pageName = String.Format(
            "{0}: Vol. {1}, No. {2}",
            e.Page.Property["Year"].ToString(),
            e.Page.Property["Volume"].ToString(),
            e.Page.Property["Number"].ToString());
        break;
}

It’s a single class file.  Change the code as-needed, and compile it into your project.  (Normal warnings apply –  I wrote this about an hour ago, and it’s only been tested for about five minutes…)

AutoNamer.zip

Sep 02, 2010

Comments

Sep 21, 2010 10:33 AM

Nice one - and elegant solution

Sep 21, 2010 10:33 AM

Very nice!

May 18, 2015 03:04 PM

Is there any way to get a version of this for newbies to EpiServer?

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024