Umbraco Backoffice default values

5 Feb 17

A feature that is really useful in umbraco is the ability to override the backoffice and give default values to your controls. 

 

This is extremely useful if you have a value that will be the same for 90% of instances.

 

In our example we will be using Umbraco version 7.

 

Let's pretend we are building a job listing website. 

 

So our document type has a job expiration date and we want it to pre-populate with 30 days by default.Closing date

 

You will need to create a class in your models folder and override the ContentService_Created event.

 

Here's how the code will look:

 

(I have named my class umbEvents, but you can name yours whatever you like).


public class umbEvents : IApplicationStartupHandler { public umbEvents() { ContentService.Created += ContentService_Created; } void ContentService_Created(IContentService sender, Umbraco.Core.Events.NewEventArgs e) { if (e.Alias == "Jobitem") { e.Entity.SetValue("closingDate", (DateTime.Now.AddDays(28))); } } }

 


You can now set a break point and step through your code.





Comments

Darren Welch

By

Darren Welch


  5 Feb 17


Contact me online

 Facebook

 Twitter

 Linked In

 Git Hub