views:

1117

answers:

3

Hi all!

I'm trying to change the default values in the "time" drop down list that the DateTimeControl displays in Sharepoint. I want an increment of 15, not 5 minutes. Anybody has any idea how this could be done? Can I overload a method or something?

+1  A: 

Unfortunately this is not possible using the out of the box DateTime field.

A SharePoint field is made up of 2 main parts. The data structure (in code) and the various views (namely in a list, new/edit/view, admin [when adding to a list]). The data structure out of the box is a standard .NET DateTime field. Unfortunately the views only give the increment by 5 minutes.

You can create your own by inheriting from the default field. MSDN has a decent explaination of how. Nick Sevens has a much clearer explanation.

Unfortunately (as with most SharePoint customizations) creating your own field in CAML can be tricky.

This project on CodePlex might be a good starting point. It's licensed under the GPL so you can modify it.

Mark
+1  A: 

As a matter of fact both the time drop down and its initializers are implemented as private data members of the DateTimeControl class so you can not change the values directly. However, the minutes drop down is prepared inside OnPreRender, we can get the control and reset its values indirectly to get desired behavior. Here is one approach

public class MyDateTimeControl : DateTimeControl
{
    protected override void Render(HtmlTextWriter output)
    {
        DropDownList minuteControl = null;
        string[] newMinutesRange = new string[] { "00", "15", "30", "45" };
        string[] newMinutesRangeExt = new string[] { "00", "15", "30", "45", "" };
        int index = 0;
        int selectedMinutes;

        try
        {
            if (!this.DateOnly && this.Controls.Count == 4)
            {
                minuteControl = (DropDownList)this.Controls[2];
            }
        }
        catch { }

        if (minuteControl != null && !this.DateOnly)
        {
            selectedMinutes = Convert.ToInt32(minuteControl.SelectedValue);
            if (selectedMinutes % 15 > 0)
            {
                index = 4;
                newMinutesRangeExt.SetValue(selectedMinutes.ToString(), index);
                newMinutesRange = newMinutesRangeExt;
            }
            else
            {
                index = selectedMinutes / 15;
            }

            minuteControl.Items.Clear();
            minuteControl.SelectedIndex = 0;
            minuteControl.DataSource = newMinutesRange;
            minuteControl.DataBind();
            minuteControl.SelectedIndex = index;
        }

        base.Render(output);            
    }
}

Hope this helps

Faheem
Hey thanks, it "almost" worked. There is just a little glitch that displays the hours like : 00-15-30-45-40 I don't know where that 40 comes from, I'm going to try to find out :)
and it doesn't keep the selected value. It's hard to override a class when you don't have access to the sources! :P
The line "this.SelectedDate.Minute" always gives the current time... weird.
It happens when you don't select a Date from the control. If you select a date, it gives me an error. Ah well :(
mistermuggles, 40 is displayed in that way just to set the default behavior of DateTimeControl and that helps you use the current minutes as compared to fixed.For the glitch, I have edited the answer above to take care of state.
Faheem
I wonder if your question was answered. Can you please share any details?
Faheem