blog.scoreman.net

Archive for the ‘SharePoint 2007’ Category

Using SharePoint DatePicker inside a custom field type or an application page

Tuesday, February 5th, 2008

I wanted to have a custom field type with a date picker and some additional controls. It wasn’t hard to integrate the standard date picker into a custom field type. Below is an extract of the code I wrote.

<%@ Control Language="C#" Debug="true" %>
<%@Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
<SharePoint:RenderingTemplate ID="myFieldControl" runat="server">
    <Template>
        <SharePoint:DateTimeControl ID="myDateTimeControl" DateOnly="True" runat="server"></SharePoint:DateTimeControl>
    </Template>
</SharePoint:RenderingTemplate>

 

public override object Value
{
    get
    {
        EnsureChildControls();

        if (!myDateTimeControl.IsDateEmpty)
        {
            return dateTimeTo.SelectedDate;
        }
        else
        {
            return null;
        }
    }

    set
    {
        EnsureChildControls();

        if (value != null)
        {
            myDateTimeControl.SelectedDate = (DateTime)value;
        }
    }
}