SharePoint 2007 Custom Date Time Field to default the time to the current time
Saturday, September 29, 2007 at 02:01PM 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"?>
<FieldTypes>
<FieldType>
<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=1.0.0.0, Culture=neutral, PublicKeyToken=XXXPUBLICKEYXXX</Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="Filterable">TRUE</Field>
</FieldType>
</FieldTypes>
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.
Reader Comments (17)
thanks for your great post.
how to use this approach for internal DateTime type like this :
<FieldType>
<Field Name="TypeName">DateTime</Field>
<Field Name="InternalType">DateTime</Field>
<Field Name="TypeDisplayName">$Resources:core,fldtype_datetime;</Field>
<Field Name="SQLType">datetime</Field>
<Field Name="FieldTypeClass">DefaultDateTimeFieldType, XXXASSEMBLYNAMEXXX, Version=1.0.0.0, Culture=neutral, PublicKeyToken=XXXPUBLICKEYXXX</Field>
<Field Name="ParentType"></Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="Filterable">TRUE</Field>
</FieldType>
If you display both date/time as an option to enter, you see date widget with separate dropdowns for hour and minutes, the minutes being in increments of 5, Hence the problem trying to assign a correct time to an issue is not easily done.
sorry for my bad english!
i want to change FieldTypeClass for internal datetime FieldType and not add any custom FieldType
Instead, you should override the GetFieldValue -method like this:
public override object GetFieldValue(string value)
{
return DateTime.Parse(value);
}
I figured this out after a half a day reflector session :-P Part of the field type handling is hardcoded in the SPListItem class, but only for the build in field types. That's why custom SPField -deriving classes must behave differently than the build in ones, which seems quite a messy design to me.
I am using Room & equipment reservation template. To reserve a room i have start date and end date. what i need is...
Start date : Should be equals to or greater than today's date & time. system should not allow any reservation done less then today's date & time.
how can i do that.
any help will be highly appreciated.
Thank you
if you don't mind. can you share the step by step instruction on how to achieve this. i've tried out your solution but it didnt work. i wonder if i've missed things. since i'm very new with sharepoint and i need to create custom default value for my custom list fields. thanks