SharePoint 2007 Custom Date Time Field to default the time to the current time

The built in Date Time Column type for SharePoint 2007 lists has an interesting problem that I was surprised to find out about. When you create a new Date Time column, you are given the option to set the default value to Today's Date. This works fne if all you care about is the date, but if you want the time to default to the current time you are out of luck. Every time you create a new list item, the time for that column will always be defaulted to 12:00 AM.

Being the developer that I am, I looked for a solution but didn't find any point and click work arounds so I resorted to creating by own custom field type.

It looked straight forward enough, I only needed to write one class, Gac it and create one xml file and add it to

The Class:

    public class DefaultDateTimeFieldType : SPFieldDateTime
        public DefaultDateTimeFieldType(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
            this.Type = SPFieldType.DateTime;

        public DefaultDateTimeFieldType(SPFieldCollection fields, string typeName, string displayName)
            : base(fields, typeName, displayName)
            this.Type = SPFieldType.DateTime;

        public override string DefaultValue
            get { return DateTime.Now.ToString("MM/dd/yyyy hh:mm tt"); }
            set {  }

        public override object DefaultValueTyped
            get { return DateTime.Now; }

You may notice that I am setting the Type property in each of the classes constructors to SPField.DateTime. It was an educated guess that had me add that line of code. Originally I didn't have it and the field worked fine until someone tried to edit a list item that contained the custom field type. You can create a new list item with no problem. You could view the values of existing items with no problems. But as soon as you tried to edit the list item, it threw an invalid cast exception.

I started looking through all the properties of the base type, and made a good guess, because as soon as I added that line of code it worked.

Anyway, besides gacing an assembly with that class, you also need to to add an xml file to the following directory: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\template\XML

The contents of the file are as follows:

<?xml version="1.0" encoding="utf-8"?>
        <Field Name="TypeName">DefaultDateTime</Field>
        <Field Name="ParentType">DateTime</Field>
        <Field Name="TypeDisplayName">Default Date Time</Field>
        <Field Name="TypeShortDescription">Default Date Time</Field>
        <Field Name="UserCreatable">TRUE</Field>
        <Field Name="ShowInListCreate">TRUE</Field>
        <Field Name="ShowInSurveyCreate">TRUE</Field>
        <Field Name="ShowInDocumentLibrary">TRUE</Field>
        <Field Name="ShowInColumnTemplateCreate">TRUE</Field>
        <Field Name="SQLType">datetime</Field>
        <Field Name="FieldTypeClass">DefaultDateTimeFieldType, XXXASSEMBLYNAMEXXX, Version=, Culture=neutral, PublicKeyToken=XXXPUBLICKEYXXX</Field>       
        <Field Name="Sortable">TRUE</Field>
        <Field Name="Filterable">TRUE</Field>

In order to GAC you need to strong name your assembly. Update the xml file above to reflect the public key and assembly name you are using. Remember to reset IIS after putting your dll in the gac and adding this xml file.

One caveat to this approach is that the column will be uneditable in DataSheet mode. This is a problem with all custom field types.

Happy coding.

Using a Custom List to Store Configuration Data

E-Mail Hosting