Try our conversational search powered by Generative AI!

Deane Barker
Sep 2, 2010
  4758
(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 Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024

Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog