Can't clear DocumentPublishTo field in code

Dan McDevitt asked on April 28, 2016 19:23

Our company uses a custom event application that allows our users to build events that are simultaneously created both in Kentico and our AMS database. To prevent the events from being published before they're ready when the event is initially created I set the DocumentPublishTo field of the event node to the day before the event was created. When the event is completely set up, the user goes to a publishing form with two calendar controls, one for the publishing date (DocumentPublishFrom) and one remove date (DocumentPublishTo).

  • Each calendar has a button that removes the selected date from it when the user clicks the button. (Actually sets the date to the default date of 1/1/0001, because the SelectedDate property of an ASP.Net calendar control is non-nullable).

  • When the form is saved, the node is updated with the dates from the calendars.

  • If the DocumentPublishFrom date is set to 1/1/0001, node.Update() sets the value of that field in the database to null. That's great.

  • If the DocumentPublishTo field is set to 1/1/0001, node.Update() does not set the value of the field in the database to null. It's set to Jan. 1, 0001, which, being less than the current date, means the event can never be published.

Does anyone know why these two fields don't behave the same way, and how I can get around this and null out the DocumentPublishTo field? For now I can modify my code to set the DocumentPublishFrom date 1 year in the future instead of setting the remove date one date in the past, but that has the drawback that if the user never finishes their event or forgets about it and leaves it there eventually it will show up online. I'd rather use the DocumentPublishTo date, which will prevent the event from ever showing up unless the creator actively changes it to do so.

Code Sample:

if (Convert.ToInt16(QueryNode) != 0)
{
    CMS.DocumentEngine.TreeNode node = TreeHelper.SelectSingleNode(Convert.ToInt16(QueryNode));

    node.DocumentPublishFrom = calPublishDate.SelectedDate;
    node.DocumentPublishTo = calRemoveDate.SelectedDate;
    node.Update();
}

Correct Answer

Roman Hutnyk answered on April 28, 2016 20:29

Have you tried something like:

node.SetValue("DocumentPublishTo", null);

3 votesVote for this answer Unmark Correct answer

Recent Answers


Dan McDevitt answered on April 28, 2016 21:03

Roman, I hadn't tried that particular formatting. I had tried

node.DocumentPublishTo = null;

But that won't work because the a datetime field can't be converted to null. Strange that it works this way.

Thank you for the suggestion.

-Dan

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.