Setting a default value for PublishingStartDate

Whenever you are using SharePoint as a publishing platform for news you will end up using some sort of DateTime fields to determine an article should be published. Out of the box you will have the Created and Modified dates that are there for you, however sometimes you will have more requirements. Now if you are using the Publishing features you will can use the PublishingStartDate and the PublishingExpirationDate. These fields are out of the box available and I urge you to use them whenever you are working with scheduled start and end dates.

However there is one caveat: setting the default value for ‘Today’ for the PublishingStartDate or setting a default value for the PublishingExpirationDate is not something you can do without some custom code. Luckily the JavaScript or jQuery required for it is fairly easy and pretty straight forward.

On the pagelayout you are using you will have to include the fields for PublishingStartDate and PublishingExpirationDate and call a JavaScript function that you can use to insert data into the fields.

<PublishingWebControls:PublishingScheduleFieldControl ID="PublishingStartDateEdit" FieldName="PublishingStartDate" InputFieldLabel="Publicatiedatum" runat="server"></PublishingWebControls:PublishingScheduleFieldControl>
<PublishingWebControls:PublishingScheduleFieldControl ID="PublishingExpirationDateEdit" FieldName="PublishingExpirationDate" InputFieldLabel="Geldig tot" runat="server"></PublishingWebControls:PublishingScheduleFieldControl>

<script type="text/javascript">
	Mavention.SP2010.Publishing.FixPublishingFields.SetDateTime();
</script>

Then you can create your JavaScript function where you have some logic to retrieve the field, and set its correct date time settings. The only thing to mind there is that the timestamp need to be rounded down to a 5 minute timespan. Furthermore the code is pretty self-explanatory. Obviously if do not want to hide fields ore want to change the default expiry date you can make these optional or configurable.

Mavention = Mavention || {
    __namespace: true,
    __typename: "Mavention"
};

Mavention.SP2010 = Mavention.SP2010 || {
    __namespace: true,
    __typename: "Mavention.SP2010"
};

Mavention.SP2010.Publishing = Mavention.SP2010.Publishing || {
    __namespace: true,
    __typename: "Mavention.SP2010.Publishing"
};

// This will provide a helper to set a date and time into the Publishing Start and EndDate
Mavention.SP2010.Publishing.FixPublishingFields = Mavention.SP2010.Publishing.FixPublishingFields || {
    __namespace: true,
    __typename: "Mavention.SP2010.Publishing.FixPublishingFields",

    SetDateTime: function () {
        // Get the block containing the StartDate
        var block = jQuery("input:[id*=startOnFollowingRadioButton]");

        // Create a new Date object that is used to set the value
        var now = new Date();

        if (block.length != 0) {
            // Hide radiobutton Immediately so users cannot click it
            jQuery("input:[id*=startTokenRadioButton]").getParent(2).css("display", "none");

            // Check if there is already a value, if not fill the field
            if (jQuery("input:[id*=_startDateTimeControlDate]").val().length == 0) {
                //click the startdate
                block[0].click();

                var min = Math.floor(parseInt(now.format("mm")) / 5) * 5; // needs to be parsed to a 5x and needs to be rounded down

                // Set the day, month and year
                jQuery("input:[id*=_startDateTimeControlDate]").val(now.format("d-M-yyyy"));
                // Set the time, will need a : to set correctly
                jQuery("select:[id*=_startDateTimeControl_startDateTimeControlDateHours]").val(now.format("HH") + ":");
                jQuery("select:[id*=_startDateTimeControl_startDateTimeControlDateMinutes]").val(min);
            }
        }

        // If ExpiryDate is available we set it with a +12 month setting making sure the expire date is a year from now
        if (block.length >= 2) {
            block[1].click();

            var dateExpired = new Date();
            dateExpired.setMonth(dateExpired.getMonth() + 12);

	    // Hide the 'never' option if needed
            jQuery("input:[id*=startTokenRadioButton]").getParent(2).css("display", "none");
	    // Set correct dd/yy
            jQuery("input:[id*=_startDateTimeControlDate]").val(dateExpired.format("d-M-yyyy"));
        }
    }
};

That way you can fairly easy set a default value for the PublishingStartDate and PublishingExpirationDate without screwing around with the field definitions and keeping all the internal SharePoint stuff that uses these fields untouched and still create a user experience where a user is provided with default values.

There are 4 comments for this article
  1. Abhi at 16:01

    I want to include this code in page layout in share point online. Where this namespace declaration go. Coudnt figure out how to make this code work. please help.

    • Albert-Jan Schot at 11:09

      What exactly do you mean by the namespace declaration? Within the PageLayout you can reference the JavaScript file that contains the code as either by including it as a hard coded reference or use a Custom Action to register it. After that it is a matter of calling the JS function. It does require jQuery loaded as well so make sure that that is available as well.

Leave a Reply