Copied to clipboard

Flag this post as spam?

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


  • Joe Codino 25 posts 125 karma points
    Mar 09, 2016 @ 11:20
    Joe Codino
    0

    First published date

    I'm using the UpdateDate to check for published date of blog posts, but if someone modifies the article few days later, i.e. correcting a typo, the UpdateDate obviously reflect the latest change so the blog post has an incorrect day value. Is there a way to get the FIRST published date, excluding all future saving? Thanks in advance, Joe

  • Mike Chambers 636 posts 1253 karma points c-trib
    Mar 09, 2016 @ 11:29
    Mike Chambers
    1

    I think that you'll find you have to go to the database for that, and interogate previous versions... If you ever clean out the audit trail/versions then you lose that, and you'll be hiting the database for front end display.

    An alternative approach is to simply add a datetime field to your blog datatype, and have the admin specify what date they want to show as the "published date" - rather than the updateDate from umbraco core.

    If you wanted to set this semi-automatic you could hook into the publishing event and if "new datetime field == empty", then set to the update date..

    that way you retain the date you want on the node, and it exists in the xml cache for the site, so no db lookups front end.

  • Aristotelis Pitaridis 84 posts 402 karma points
    Mar 09, 2016 @ 11:30
    Aristotelis Pitaridis
    1

    You can use the CreateDate property. This property defines the date and time that the content created and it is not going to change.

  • John Churchley 272 posts 1258 karma points c-trib
    Mar 09, 2016 @ 11:31
    John Churchley
    0

    Hi Joe,

    There isn't at second. The common practice is to create a property called publishedDate and get the user set it on create. I tap in to the ContentService to set it automatically on creation of the content node.

    John

  • Joe Codino 25 posts 125 karma points
    Mar 09, 2016 @ 12:30
    Joe Codino
    0

    Thanks to all. @John or @Mike, can you provide me a little example or point me to a tutorial to do that?

    @Aristotelis the CreateDate is sometime days before the content is published

  • Aristotelis Pitaridis 84 posts 402 karma points
    Mar 09, 2016 @ 12:46
    Aristotelis Pitaridis
    1

    I just wrote a class which will make what you want.

    using System;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Services;
    
    namespace UmbracoExample
    {
        public class PublishDateApplicationEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Publishing += ContentService_Publishing;
            }
    
            private void ContentService_Publishing(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                var contentService = ApplicationContext.Current.Services.ContentService;
                foreach (var content in e.PublishedEntities.Where(m => m.HasProperty("publishDate")))
                {
                    var existingValue = content.GetValue("publishDate");
                    if (existingValue == null)
                    {
                        content.SetValue("publishDate", DateTime.Now);
                    }
                }
    
            }
        }
    }
    

    This code is executed before you publish a content. It checks if the content has the property with alias name publishDate and if it has it then it checks if we have defined a value for this property. In case that we have not defined this means that this is the first publish time so we set the publish date.

    In order to make it work you will have to create a new property which will have the alias name publishDate.

  • Mike Chambers 636 posts 1253 karma points c-trib
    Mar 09, 2016 @ 12:55
    Mike Chambers
    0

    Ps.. you can just create a Events.cs (filename not important) file in the app_code folder and cut and paste @Aristotelis Pitaridis code into it..

    (can't remember now, but you might have to drop the namespace if using the JIT compiled app_code folder approach)

    Or you can create you own class library and build the solution to give you a .dll to drop into the bin folder.

    More info... https://our.umbraco.org/Documentation/Getting-Started/Code/Subscribing-To-Events/

  • Aristotelis Pitaridis 84 posts 402 karma points
    Mar 09, 2016 @ 13:04
    Aristotelis Pitaridis
    0

    You can create a XXXXXXX.cs file in the App_code folder (if the folder does not exists then you will have to create it).

    If you have the project in Visual Studio then you can create a folder and put it there and build the project which will have as a result the code will be compiled in the application's dll file.

  • Joe Codino 25 posts 125 karma points
    Mar 09, 2016 @ 13:50
    Joe Codino
    0

    Thank you! It works!

    To help other people, this is how you can get items ordered by publishDate:

    .OrderByDescending(o => o.GetPropertyValue<DateTime>("publishDate"));
    
  • Petr Hruzek 28 posts 100 karma points
    Mar 17, 2016 @ 08:26
    Petr Hruzek
    0

    Aristotelis' solution works, but it has an issue. When I click Save and Publish from within a backoffice, the publishDate value is not updated back to UI, so when I want to leave the node, I am getting "Unsaved changes" popup. Is there any solution to this?

    EDIT:

    Well, I cannot reproduce that "Unsaved changes" behavior on newly created nodes, so I take it back.

    But there is still a minor issue. The publishDate is updated back to backoffice UI only when the node is saved and published on first try (I mean first click to "Save and publish"). If there are some errors (for example missing required property), I fix them and click Save and publish button again, then now the publishDate is not updated back to UI.

    As I wrote, this is a minor issue and I can live with that, but maybe there is a simple solution how can I force UI to update with fresh node values. Thanks.

    EDIT 2:

    I am getting that "Unsaved changes" popup again, but I cannot find a way to reproduce it. Anyway, my question is if there is a way how to force backoffice UI update (publishDate property value) from within a ContentService_Publishing method?

  • Mike Chambers 636 posts 1253 karma points c-trib
    Mar 17, 2016 @ 09:27
    Mike Chambers
    0

    Sounds like a bug in the underlying core that you've found..

    Perhaps you could/should raise an issue if no one esle has reported it?

    http://issues.umbraco.org/

  • Aristotelis Pitaridis 84 posts 402 karma points
    Mar 17, 2016 @ 11:18
    Aristotelis Pitaridis
    0

    If you have problem with the publishing event then you can try the published event. Try the following code. I have not tested it but it should work.

    using System;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    using Umbraco.Web;
    
    namespace UmbracoExample
    {
        public class PublishDateApplicationEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentService_Published;
            }
    
            private void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                foreach (IContent node in e.PublishedEntities.Where(m => m.HasProperty("publishDate")))
                {
                    var existingValue = node.GetValue("publishDate");
                    if (existingValue == null)
                    {
                        var contentServices = ApplicationContext.Current.Services.ContentService;
                        var content = contentServices.GetById(node.Id);
                        content.SetValue("publishDate", DateTime.Now);
                        contentServices.SaveAndPublishWithStatus(content);
                    }
                }
            }
        }
    }
    
  • Roger Jarl 22 posts 115 karma points
    Feb 21, 2024 @ 10:47
    Roger Jarl
    0

    This worked for me:

    IEnumerable<IContent> lstVersions = Services.ContentService.GetVersions(Model.Id);
    if (lstVersions.Count() > 0)
    {
        DateTime dtFirstPublishedDate = lstVersions.Min(x => x.UpdateDate);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft