0

I'm trying to set the start- and end time when creating a new event in a calendar to 00.00 for both fields but i had little to no success as i know very little about client side coding.

I've been digging the web and only found how it was done in SharePoint 2010. The only things i came up with is that i should use the _spBodyOnLoadFunctionNames so far.

This is what i got. I have no idea how to get an element nor setting it using Javascript more than getElementByID and that i'm not sure if that would work.

<script language="javascript">   
    _spBodyOnLoadFunctionNames.push("setEventDates");   
    function setEventDates()   
    {   
        var startTime = .val("00:00");
        var endTime = .val("00:00");
    }   
</script>

Any help or pointers in the right direction would be highly appreciated.

  • Do you mean you're trying to set the TIME to 00:00, not the date? – Submits Sep 22 '15 at 8:07
  • @Submits, Correct, changed the variables to be more clear. – Christoffer Sep 22 '15 at 8:09
1

Download jQuery and place it into your Site Assets library, as shown in my example below.

<script type="text/javascript" src="../../SiteAssets/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
    _spBodyOnLoadFunctionNames.push("setEventDates");
    function setEventDates() {
        $("#ctl00_ctl34_g_4a492851_365e_4c55_a962_ba0f68262ae1_ff31_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours").val("00:");
        setTimeout(function() { $("#ctl00_ctl34_g_4a492851_365e_4c55_a962_ba0f68262ae1_ff41_ctl00_ctl00_DateTimeField_DateTimeFieldDateHours").val("00:") }, 300);
    }   
</script>

What comes after the # is the ID of the two dropdowns. I'm not sure if the ID will be the same for you so you should check by opening the new calendar event form in your browser, press F12 in your browser to bring up the developer tools, choose the tool that allows you to click elements on the page, click the dropdown and you'll see in the developer tools pane the <select>... tag, which will have an id attribute.

The timeout is needed because when the first dropdown is changed to 00 SharePoint will try to change the second date's time to 01. So you wait for SharePoint to change it to 01 (300ms) then change it back to 00. Probably not the best approach, but it works.

As a side note, if you don't want a time portion for your date-time field you can specify this in the settings for that field.

  • Thanks! Almost there as 300 milliseconds did not stop SharePoint from setting the end time to 01. Tried to set it to 100 milliseconds, and 1 seconds but it did not make any difference.. And i'm using the OOTB calendars content type so i did not want to tinker with the site column as it is used everywhere in the Sitecollection. :) – Christoffer Sep 22 '15 at 9:17
  • So the first dropdown gets set to 00 correctly? If so, it should be able to set the second as well. Try setting the timeout to a much higher value like 3000ms, just as a test. If it works, you'll need to tinker with the timeout value to get it right. – Submits Sep 22 '15 at 9:20
  • Tried with 5 seconds and downwards and tried from 100ms without any luck.. did it work for you? – Christoffer Sep 22 '15 at 10:44
  • Yep works perfectly for me. Double-check the IDs of the two elements. Currently, does the first dropdown get changed to 00 and the second is 01? – Submits Sep 22 '15 at 11:08
  • Correct, it shows '00' and '01' no matter the miliseconds/seconds that i try to delay the execute. And i checked my IDs, they are identical for both fields while your differs at ff31 and ff41 .. – Christoffer Sep 22 '15 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.