Customize DateTime in SharePoint

SharePoint users know that when creating or editing an item, you can specify the time at intervals of 5 minutes. In most cases, this is enough. However, there are such customers whom this does not suit. They want to make appointments at 12:02, demand the task to be completed by 16:31, register users ’requests up to a minute. Their right, they pay money for it.

It’s not possible to ask SharePoint to count not five minutes, but one at a time, using standard tools. Some friends who were familiar with this task danced with SharePoint Designer and Visual Studio, trying to build custom forms and / or FieldType's. In my opinion, there is a more beautiful solution. Who cares, welcome to cat.


Theory


SharePoint makes extensive use of the template system. For the list content type, the UserList template is set by default. It can easily be changed to any other one by determining the type of content or using the notorious SharePoint Manager directly on a running server. Template definitions are in % SharePointRoot% \ CONTROLTEMPLATE \ CONTROLTEMPLATES \ DefaultTemplates.ascx .
Not many beginners know that templates are also used to display element fields. However, in the Field definition it is impossible to specify which template it needs to use. We take advantage of the use of templates by SharePoint: SharePoint first loads the standard templates, then all the rest. If the identifiers of the patterns match, then the last loaded one is used for display. In this regard, we can only redefine the standard template.

Practice


The standard DateTime field template uses DateTimeControl to display . So we need to replace this particular control. We will replace the heir.

	public class CustomDateTimeControl : DateTimeControl

By decompiling the logical reasoning, we determine the identifier of the drop-down list that displays the minutes for the user and find it in the child controls:

private DropDownList _minuteDropDownList;
		protected override void CreateChildControls()
		{
			base.CreateChildControls();
			var minutesId = base.ID + "DateMinutes";
			_minuteDropDownList = FindControl(minutesId) as DropDownList;
		}

The data binding in this list occurs at the PreRender stage, therefore, after the parent class makes the binding, we will redefine this binding:

protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (_minuteDropDownList != null && !DateOnly)
			{
				_minuteDropDownList.DataSource = _minutes;// массив отображаемых минут.
				_minuteDropDownList.DataBind();
				_minuteDropDownList.SelectedIndex = SelectedDate.Minute;
			}
		}


Now create an ascx file in which the new template will be stored.


After the solution is expanded, the date field will contain a drop-down list of minutes with an interval of 1 minute.

Conclusion


It should be remembered that this decision will affect all sites of the application on which the solution is deployed.

Also popular now: