Copied to clipboard

Flag this post as spam?

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


  • Chris Norwood 14 posts 175 karma points
    Jan 23, 2018 @ 11:17
    Chris Norwood
    0

    Refreshing back office after saving event

    Hi,

    I have the following code, designed to catch if a user has failed to enter value for the "publicationDate" field on a document:

        public class ContentSavingEventHandler : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            base.ApplicationStarted(umbracoApplication, applicationContext);
            ContentService.Saving += ValidateContent;
        }
    
        private void ValidateContent(IContentService sender, SaveEventArgs<IContent> e)
        {
            if(e.SavedEntities.Count() == 1)
            {
                var saveEntityEventArgs = e.AsEnumerableOfOne().First().SavedEntities;
                var content = saveEntityEventArgs.First();
    
                if(content.HasProperty("publicationDate") 
                    && content.Properties["publicationDate"].Value == null)
                {
                    content.SetValue("publicationDate", DateTime.Today);
                }
            }
        }
    }
    

    This works as expected, in as much as the data is saved and if it's not present the publication date field value is set to today's date.

    However, this isn't reflected in the back office for the document after saving - the "publication date" field is null.

    (I would personally prefer an approach where the publication date is mandatory, but the client has insisted that it is set to today's date if it's null).

    I can't find any way of getting the data into the backoffice document without refreshing the document manually (I've searched around here and seen various threads on similar topics but none seem to have a solution).

    Is there some way of doing this? I know the client is going to ask why the value isn't reflected in the date picker without refreshing...

  • Marc Goodson 617 posts 3810 karma points MVP 3x c-trib
    Jan 27, 2018 @ 09:22
    Marc Goodson
    100

    Hi Chris

    What I tend to do in this situation is set the default date to be today's date before the document is displayed for the editor to begin editing.

    The editor is then aware that it has been set to be today's date and they have the option to change it.

    If you allow them to leave it blank, and set it todays date on Save or Save and Publish, then it's not transparent to the editor what is happening.

    And then you can also set the date field to be mandatory, to prevent it being manually blanked!

    Anyway the secret way of doing this, don't tell anyone else... is to hook into the EditorModelEventManager SendingContentModel event...

    ... this fires just before the editor loads in the backoffice, and contains a model representing the properties and tabs that the particular doctype you are creating/editing requires, and any values to display... enabling you to tweak them in their state between database and presentation to the editor...

    ... so it's the perfect place to set a default value.

    Something like this example, I think would work for you:

    protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;   
        } 
    
     private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
            {
    
            //set a default value for NewsArticle PublishDate property, the editor can override this, but we will suggest it should be todays date
            if (e.Model.ContentTypeAlias == "newsArticle")
            {
               var pubDateProperty = e.Model.Properties.FirstOrDefault(f => f.Alias == "publishDate");
                if (pubDateProperty.Value == null || String.IsNullOrEmpty(pubDateProperty.Value.ToString())){
                //set default value if the date property is null or empty
                    pubDateProperty.Value = DateTime.UtcNow;
                }
            }
    

    regards

    Marc

  • Chris Norwood 14 posts 175 karma points
    Jan 27, 2018 @ 10:18
    Chris Norwood
    0

    Hi Marc,

    Thank you - that was my other potential solution (setting today's date at creation of the item) but I didn't find anything about that event despite a lot of hunting around - I'm relatively new to Umbraco .

    I wanted to make the field mandatory so the user couldn't forget to set it (typically these articles will have an older date than today), but the client has asked for it not to be, so this is the best solution I think :).

    Thanks again!

  • Marc Goodson 617 posts 3810 karma points MVP 3x c-trib
    Jan 27, 2018 @ 12:36
    Marc Goodson
    0

    Cool, yes, I've coincidentally added details of this 'hidden feature' to the Umbraco docs, so it should be discoverable for all soon...

    https://github.com/umbraco/UmbracoDocs/pull/582

    but should work well in your scenario.

Please Sign in or register to post replies

Write your reply to:

Draft