Umbraco Backoffice default values

Posted 14 October 2018 by Darren Welch

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.





Darren Welch

By

Darren Welch


  14 October 2018


Contact me online

Advertisment


Tags


umbraco code programming code

Related Content




Connect to SQl Server from command prompt

19 July 2016




Toastr - a useful message javascript library

20 Oct 2016




Send ctrl alt delete over remote desktop

13 Jan 17