This SharePoint tutorial explains, how to set a default date to SharePoint DateTime control in SharePoint 2013/2016 using SharePoint Server Object model code. Here we will set the default date as today’s date to the SharePoint DateTime control in SharePoint 2013/2016.
User want to see the default values while pulling out the loan details report with the given start and end date. Where the start date should be today’s date and end date should be “12/31/8888” in a server side WebPart control. When user click on Submit button we should ensure that End date is not less than Start Date.
We can implement this in both CSOM and c#.net. Since the requirement is to implement in a server side control. Below is the code snippet to achieve this:
SharePoint Date Control:
<SharePoint:DateTimeControl ID=”cldStartDt” runat=”server” />
<SharePoint:DateTimeControl ID=”cldEndDt” runat=”server” />
<asp:Label ID=”lblMessage” runat=”server” ></asp:Label>
C# code to populate default values:
cldStartDt.SelectedDate = Convert.ToDateTime(DateTime.Today.ToString());
cldEndDt.SelectedDate = Convert.ToDateTime(DateTime.MaxValue.AddYears(-1111).ToString());
“Convert.ToDateTime(DateTime.Today.ToString())” with this we can get today’s date in Date and Time format.
Convert.ToDateTime(DateTime.MaxValue.AddYears(-1111).ToString()) with this we get the end date as “12/31/8888”
Validation to ensure that EndDate is not less than Start Date:
Which the below code snippet we can achieve the validation.
int dtDiff = Convert.ToInt32(endDate.Date.Subtract(Convert.ToDateTime(cldStartDt)).Days);
lblMessage.Text = “End Date must be greater than Start Date!!”;
You may like following SharePoint tutorials:
- Activate and Deactivate SharePoint Server Publishing feature using .Net managed object model in SharePoint 2016
- Export SharePoint 2013 List Items to Word Document using SharePoint Server Side Object Model
- SharePoint online Read csv file from document library using .Net managed client object model csom
- Create a folder and subfolder inside document library using .Net client object model in SharePoint online
- Create Site Columns programmatically in SharePoint 2016/2013 using Server Object Model in Visual Studio 2017
- Upload file to SharePoint 2016 document library with metadata programmatically using file upload control (Visual Web Part + Server Object Model)
- SharePoint 2013: Add, Delete and Display User Custom Actions using SharePoint Server Object Model
Hope this SharePoint tutorial helps to set today’s date to SharePoint date picker using SharePoint 2013/2016 server object model code.
I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019
Download SharePoint Online Tutorial PDF FREE!
Get update on Webinars, video tutorials, training courses etc.