Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Umbraco Document Type Field Default Value

Tags:

umbraco

I would like set a default value for a date picker field in a Document Type in Umbraco.

How can I do this?

like image 498
Nami Avatar asked Mar 15 '26 10:03

Nami


1 Answers

This can be easily done using Events, on Document.New

http://our.umbraco.org/wiki/reference/api-cheatsheet/using-applicationbase-to-register-events/overview-of-all-events

Just create a new class (eg. UmbracoEvents.cs)

using System;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using Examine;

public class UmbracoEvents: ApplicationBase
{
  /// <summary>Constructor</summary>
  public UmbracoEvents()
  {
    Document.New += new Document.NewEventHandler(Document_New);
  }

  private void Document_New(Document sender, umbraco.cms.businesslogic.NewEventArgs e)
  {
    if (sender.ContentType.Alias == "News")
    {
      sender.getProperty("date").Value = DateTime.Today; // Set the date for a new News item to today
    }
  }
}
like image 82
Aximili Avatar answered Mar 17 '26 04:03

Aximili



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!