Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Morten Jensen 41 posts 104 karma points
    Apr 09, 2015 @ 14:34
    Morten Jensen
    0

    Set a Date/Time property in code

    I have created a "Date Picker With Time" property on my NewsItem documents types, to handle a user editable date for news items.

    I do however want that field to automatic get set to the time of the first publish. So i created a CustomEventHandler like this.

    public class CustomEventHandlers : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Published += ContentServicePublished;
        }
        private void ContentServicePublished(IPublishingStrategy sender, PublishEventArgs<IContent> args)
        {
            foreach (IContent node in args.PublishedEntities)
            {
                if (node.HasProperty("firstPublished") && node.GetValue("firstPublished") == null)
                {
                    node.SetValue("firstPublished", DateTime.Now);
                }
            }
        }
    }

    But guess what? I won't work. I have debugged the code and the line "node.SetValue" is running? Does the "Date Picker with Time" dont use DateTime data?

  • Morten Jensen 41 posts 104 karma points
    Apr 09, 2015 @ 14:39
    Morten Jensen
    101

    Found the error.

    You can of couse only set values on the Publishing event, not the Published event i used..

Please Sign in or register to post replies

Write your reply to:

Draft