📜 ⬆️ ⬇️

DateTime customization in SharePoint

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

It is not possible to ask SharePoint to count not five minutes, but one by one, standard means. Some people familiar with this task were dancing with SharePoint Designer and Visual Studio, trying to build custom forms and / or FieldTypes. In my opinion, there is a more beautiful solution. Who cares, welcome under cat.


Theory


SharePoint is actively using the template system. For the list content type, the default UserList template is set. It can easily be changed to any other through the definition of the type of content or using the well-known 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 the fields of elements. However, in the definition of a Field, it is impossible to specify which template to use. Let's use the feature of using SharePoint templates: SharePoint first loads standard templates, then all the others. If the identifiers of the templates match, then the last loaded one is used for display. In this regard, we can only redefine the standard template.
')

Practice


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

public class CustomDateTimeControl : DateTimeControl 

By decompiling logical reasoning, we determine the identifier of the drop-down list that displays the minute to 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, so after the parent class has made a binding, we will override 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.
 <SharePoint:RenderingTemplate ID="DateTimeField" runat="server"> <Template> <CustomTag:CustomDateTimeControl runat="server" ID="DateTimeField"/> </Template> </SharePoint:RenderingTemplate> 


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

Conclusion


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

Source: https://habr.com/ru/post/169167/


All Articles