Monday 7 July 2014

Custom Data Field in sharepoint

Date time data field in sharepoint
If you add datetime column to list and assign todays datetime, it will take 12.00AM by default. But if you want accurate time,
we have to go for custom data type.
Steps:
1.Class file
2. Add to dll to GAC
3.Create a XML file
Create a class file and add DLL to GAC
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; }
}
}
Add above class file DLL to GAC.In order to GAC you need to strong name your assembly
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:
DefaultDateTime
DateTime
Default Date Time
Default Date Time
TRUE
TRUE
TRUE
TRUE
TRUE
datetime
DefaultDateTimeFieldType, XXXASSEMBLYNAMEXXX, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=XXXPUBLICKEYXXX
TRUE
TRUE
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.