How to stop someone changing the name of an Umbraco page

Posted written by Paul Seal on September 23, 2020 Umbraco

My friend Nik asked me today if I knew how to prevent an umbraco node from being saved if the name has changed.

I hadn't done it before but I knew how to intercept saving events, so I knocked up this quick example for him in the v8 project I was working on.

And because I didn't need it in the project I was working on and I knew I might want it in the future, I decided to create this blog post with the code in it.

In this example we are prevent anyone from changing the name of the home page. You can change the Content Type Alias to meet your needs.

using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Events;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;

namespace CodeShare.Core.Composing
{
    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class SavingEventComposer : ComponentComposer<SavingEventComponent>
    { }

    public class SavingEventComponent : IComponent
    {
        public void Initialize()
        {
            ContentService.Saving += ContentService_Saving;
        }

        private void ContentService_Saving(IContentService sender, ContentSavingEventArgs e)
        {
            foreach (var node in e.SavedEntities)
            {
                // if it is a home page doc type and it isn't new and the name has changed
                if(node.ContentType.Alias == "homePage" &amp;&amp; node.Id > 0 &amp;&amp; node.IsPropertyDirty("Name"))
                {
                    e.Cancel = true;
                    e.CancelOperation(new EventMessage("Error", "You can't change the name", EventMessageType.Error));
                }
            }
        }

        public void Terminate()
        {
            // Nothing to terminate
        }
    }
}

I hope it helps someone in the future, even if it is me 😃