Try our conversational search powered by Generative AI!

Deane Barker
Sep 2, 2010
  4756
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog