Automatic property population when node created

Pre-populate properties right after node is created

  • Share

For pretty much every website I built in Umbraco, there is some sort of title property for page document type. Either it is a title for navigation in breadcrumbs or menu or main h1 page title.

Usually some of these properties need to be mandatory and in most cases they are the same as the name of the node in content, but still, you have to keep ability to override the value with these properties.

I found this really annoying and in the end time consuming for content managers. That's why I decided to put some automation in Umbraco default behavior.

To actieve this, your code first needs to hook to Umbraco events. Starting from Umbraco 6, this is possible by creating a class which inherits Application handler and hooking to events through ContentService. The following is the code to hook to Umbraco ContentCreated event.

 public class ApplicationHandler : ApplicationEventHandler
    {
        public ApplicationHandler()
        {
            ContentService.Created += new Umbraco.Core.Events.TypedEventHandler<IContentService, Umbraco.Core.Events.NewEventArgs<IContent>>(ContentService_Created);
        }
		
		void ContentService_Created(IContentService sender, Umbraco.Core.Events.NewEventArgs<IContent> e)
        {
			//Logic goes here
		}
	}
    

All you have to do now is to target the properties which value you want to set with node name. It is useful to keep the properties as a list or just siply save them as comma separated values in web.config.

<configuration>
	<appSettings>
		<add key="AutoPopulateProperties" value="title,pageTitle,navigationTitle" />
	</appSettings>
</configuration>
    
Note

It is never a good practice to hardcode value

Now just to iterate through properties list, check if saved content has that property defined and set the value same as node name

    public class ApplicationHandler : ApplicationEventHandler
    {
        public ApplicationHandler()
        {
            ContentService.Created += new Umbraco.Core.Events.TypedEventHandler<IContentService, Umbraco.Core.Events.NewEventArgs<IContent>>(ContentService_Created);
        }

        void ContentService_Created(IContentService sender, Umbraco.Core.Events.NewEventArgs<IContent> e)
        {
            foreach (string propertyName in System.Web.Configuration.WebConfigurationManager.AppSettings["AutoPopulateProperties"].Split(','))
            {
                if (e.Entity.HasProperty(propertyName))
                {
                    e.Entity.SetValue(propertyName, e.Entity.Name);
                }
                ApplicationContext.Current.Services.ContentService.Save(e.Entity, 0, false);
            }
        }
    }
    
  • Share

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments for this article

comments powered by Disqus