EPiServer 7 adds many great new features but the new edit mode is really different from "old" edit mode. Personally I think EPiServer 7 edit mode is easier for editors new to EPiServer but some seasoned editors may still be very comfortable with "old" edit mode (at least for a short while). 

I was discussing this with a client and they thought that we could upgrade an EPiServer 6 R2 site to EPiServer 7 quicker than they could arrange training for all of their editors for EPiServer 7 (the client has editors in several offices across the world).

So I set out working out how to hide the new edit mode for selected editors (at least for a short period anyway).

Why would you do it?

The primary reason I can think of is training. The new edit mode is great but a large organisation with lots of editors might take some time to train all of the editors.

How do I do it?

We can use a EPiServer initialisation module that implements IInitializableHttpModule that allows us to hook into page events and send editors to the "right" edit mode. If a user is not in the defined group then they will always be pushed to the old edit mode.

The code is pretty simple and is only designed to be temporary!

[InitializableModule]
[ModuleDependency((typeof(EPiServer.Web.InitializationModule)))]
public class EditModeVersionSelectorInit : IInitializableHttpModule
{
    public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context) { }
    public void Preload(string[] parameters) { }
    public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context) { }

    public void InitializeHttpEvents(System.Web.HttpApplication application)
    {
        application.PreRequestHandlerExecute += application_PreRequestHandlerExecute;
    }

    void application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Get hold of the right context
        HttpContext context = ((HttpApplication)sender).Context;

        // First check if the user is authenticated
        if (context.User.Identity.IsAuthenticated)
        {
            string UIUrl = this.getUIUrlPath();

            // Find out if we are serving a page in the EPiServer UI
            if (context.Request.RawUrl.StartsWith(UIUrl, StringComparison.InvariantCultureIgnoreCase))
            {
                if (this.isInNewEditMode(context.Request.RawUrl))
                {
                    string newEditModeAllowedRole = "CMSNewEditModeUsers";

                    // Allow the role name to be set in config if we want to override in config
                    if (ConfigurationManager.AppSettings["NewEditModeAllowedRole"] != null 
                        && !String.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["NewEditModeAllowedRole"].ToString()))
                    {
                        newEditModeAllowedRole = ConfigurationManager.AppSettings["NewEditModeAllowedRole"].ToString();
                    }

                    // Now check if the user is in the right group
                    if (!context.User.IsInRole(newEditModeAllowedRole))
                    {
                        // User is not in the right role so send them to old edit mode
                        context.Response.Redirect(UIUrl + "edit/");
                    }
                }
            }
        }
    }

    private bool isInNewEditMode(string Url)
    {
        return Url.EndsWith("/home", StringComparison.InvariantCultureIgnoreCase) || 
            (Url.EndsWith("/") && !Url.EndsWith("edit/", StringComparison.InvariantCultureIgnoreCase));
    }

    private string getUIUrlPath()
    {
        // Parse out the segments we need to identify if this is a request for an EPiServer UI page

        if (EPiServer.Configuration.Settings.Instance.UIUrl.IsAbsoluteUri)
        {
            // OK there is an absolute path
            return EPiServer.Configuration.Settings.Instance.UIUrl.LocalPath;
        }
        else
        {
            // We have have a relative path
            return VirtualPathUtility.ToAbsolute(EPiServer.Configuration.Settings.Instance.UIUrl.ToString());
        }
    }
}

This ensures that only editors in the "CMSNewEditModeUsers" group can access new edit mode, all other editors are pushed to old edit mode.

Health warning

EPiServer do not guarantee to support for old edit mode so this should be treated as a short term solution until all editors are happy/trained in new edit mode. This code has also only had a rudimentary amount of testing.

Conclusion

Do you think your clients will encounter this challenge? Do you think this code is useful and/or should be made into an add-on? Have you encountered this problem yourself?

I'd be happy to hear your anwers to the questions above on the comments below or on @davidknipe


Comments