Copied to clipboard

Flag this post as spam?

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


  • David Armitage 505 posts 2073 karma points
    Oct 22, 2016 @ 08:30
    David Armitage
    0

    Umbraco Events - Programmatically Updating Dates

    Hi,

    No so much of a big problem but just wondered if any knows if there is a solution.

    1. I have an event which does a couple of things in the background when a user saves or publishes a node.

    2. Usually I use this to do things like auto generate reference numbers. To save the the user having to type this out when adding each node.

    3. Other things I do it rename the node name based on the node content. I like to this is it enforces a standardised naming structure for the nodes and helps later when search nodes in the list view.

    This all works great and what I like is that if I use the publishing event to rename the node name or text fields, boolean fields for instance the user can see this happen in front of them. Umbraco seems to push the new values to view without having to refresh the node (click on and off the node) - AWESOME.

    1. The issue is with Date Fields. I can update them no problems within the events but the new value doesn't seem to get pushed to view and to see the changes the user literally has to click off the node and then back on to force a refresh.

    2. It would be awesome of I could get to to work. When the user clicks save or publish I use the event to auto populate a date field and then this gets pushed to view without having to manually refresh the node.

    Any help would be much appreciated. As I said it is not a big problem but it would improve my user experience.

    Kind Regards

    David

  • David Armitage 505 posts 2073 karma points
    Oct 22, 2016 @ 08:31
    David Armitage
    0

    Oh and I forgot to mention I have also tested adding the login to different events such as saving, publishing to see if this makes a difference. This didn't seem to work.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 24, 2016 @ 12:07
    Dan Diplo
    0

    If I understand correctly, you should be able to use the ContentService_Created event to do this (I use it for something similar).

    So the code below will register the event in application started and then when any new content is created will check if it has a property called "eventDate" and, if so, populate with the current date.

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ContentService.Created += ContentService_Created;
    }
    
    private void ContentService_Created(IContentService service, NewEventArgs<IContent> e)
    {
        var doc = e.Entity;
    
        if (doc.HasProperty("eventDate"))
        {
              doc.SetValue("eventDate", DateTime.Today.Date);
        }
    }
    

    See https://our.umbraco.org/Documentation/Reference/Events/ContentService-Events

  • David Armitage 505 posts 2073 karma points
    Oct 24, 2016 @ 12:19
    David Armitage
    0

    Thanks. I am not sure this is exactly what I am looking for.

    1. Yes I use similar event code to update my date field. I actually use published rather than created because I need to populate a date field when updating a node rather than creating.

    2. Anyway the issue is not saving the new value. I can do this without a problem.

    3. The problem is if I use similar code to your example to save basic form fields like a text box or checkbox then it saves the data in the database etc then it also updates the screen with the new values without having to preform a page refresh or click of the node and back on to see the new value.

    4. If I use the same code to update a date field it updates the new value into the database etc but it doesn't refresh the screen with the new value. To see the new value I have to refresh the screen or click off and back onto the node to load the new values.

    5. I just want the date fields to auto refresh to screen view after I have updated these values programmatically like it does with the other form field types.

    Kind Regards

    David

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 24, 2016 @ 12:29
    Dan Diplo
    0

    I've also manipulated dates in the publishing event and this has worked fine and they display back in the editor view for the node. Note that I use the ContentService.Publishing event for this and not the ContentService.Published event. Have you tried using the former?

    The code below is what I use to ensure for events I create that if the editor doesn't enter an end date for the event then it is set to the same as the start date. This works fine and is reflected instantly in the UI after publish.

    public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ContentService.Publishing += ContentService_Publishing;
    }
    
    private void ContentService_Publishing(IPublishingStrategy service, PublishEventArgs<IContent> e)
    {
        foreach (var doc in e.PublishedEntities)
        {
            CreateEventEndDate(doc, e);
        }
    }
    
    private static void CreateEventEndDate(IContent doc, PublishEventArgs<IContent> e)
    {
        /* For events, set the endDate same as the startDate */
    
        if (doc.ContentType.Alias != "Event" || !doc.HasProperty("endDate"))
            return;
    
        if (doc.GetValue("endDate") == null && doc.HasProperty("startDate"))
        {
            doc.SetValue("endDate", doc.GetValue("startDate"));
        }
    
        if (doc.HasProperty("startDate"))
        {
            var startDate = doc.GetPropertyAsDateTime("startDate", DateTime.MinValue);
            var endDate = doc.GetPropertyAsDateTime("endDate", DateTime.MaxValue);
    
            if (startDate > endDate)
            {
                e.Messages.Add(new Umbraco.Core.Events.EventMessage("Event Calendar", "The event end date should not be before the start date", Umbraco.Core.Events.EventMessageType.Error));
            }
        }
    }
    
  • David Armitage 505 posts 2073 karma points
    Oct 24, 2016 @ 12:38
    David Armitage
    0

    Hi Dan,

    That's strange. Yeah I use pretty much the same code to do this job but date fields just don't seem to pull through to the editor.

    I will compare your code with mind but it doesn't look like there is any notable differences.

    It maybe that I am updating dates that already have a value whereas you are updating a date with a blank value. There maybe be some bug creating differences.

    I will give both instances a try to rule that out.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 24, 2016 @ 13:08
    Dan Diplo
    0

    The only other thing I can think of is that my date fields are in a second tab, which is not initially displayed. Maybe, somehow, switching tab loads the data into the UI? I've seen other Umbraco bugs related to stuff load or not loading when not in the primary tab.

  • David Armitage 505 posts 2073 karma points
    Oct 25, 2016 @ 02:45
    David Armitage
    0

    Yeah fair point I believe they are not on the first tab. I will check this at the same time. Thanks for your input.

    Kind Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft