Copied to clipboard

Flag this post as spam?

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


  • Pavel Gurecki 55 posts 158 karma points
    Mar 28, 2013 @ 17:44
    Pavel Gurecki
    0

    Sorting content by "Last published" date

    Hello,

    I've bumped into content sorting problem with Razor using Umbraco 6 mvc (in PartialView macro).

    Each piece of content have two date properties exposed

    • CreateDate
    • UpdateDate
    None of them can be used to correctly sort content as I want to use latest publish date.
    "CreateDate" can't be used together with "Publish at" date setting. "UpdateDate" can't be used if you plan to fix or somehow update old posts without changing sort order.
    Example:
    I create post1 on Monday and want it to be published on Friday. I have post2 created and published on Tuesday. After friday publishing sort order should be: 
    1. post1
    2. post2
    Problems:
    • If sorted by update date: I fix misstype in post2 on saturday, sorting will be broken
    • If sorted by create date: post1 will never have higher position than post2 as it was created earlier.
    I've found a lot of threads about this problem with no solution for razor.
    Umbraco.Core.Models.Content
    Exposes "ReleaseDate" but I guess it gets blank after auto publishing so it has no use here.
    So if  there is any way to get "Last published" date for content node so further sorting can be implemented?

  • Curetos 7 posts 32 karma points
    Mar 28, 2013 @ 19:44
    Curetos
    0

    Hi Pavel,

    There may be an easier way to do it, but this is how I would tackle the problem:

    I would create the property SortDate, and another true/false property PublishMode.  While SortDate is self-explanatory, I would set PublishMode to true once you are done drafting and adding content to a page.  So if I've written a page and only need to fix typos and reword after it has been published, this would be set to true.

    I would then hook into a republish event and add some logic like so:

    if (page's type alias is a post) && (PublishMode is false) {
    if (DateCompare(page.CreateDate, page.UpdateDate) >= 0 
    { page.SortDate = page.CreateDate } 
    else {page.SortDate = page.UpdateDate}
    }

    So then after you're done, you can have a date that will be sorted depending on the "mode" of the document while it is being republished.

    --Edit--

    As a side note, even if you don't have a date picked, the datepicker datatype automatically sets itself to something like "01/01/0011".  So it would never return a null value.

    Also, the datepicker datatype is not a DateTime object.  So to use the DateCompare, you have to parse each date property into a DateTime object like:

    DateCompare(DateTime.Parse(Page.CreateDate.ToString("r"), DateTime.Parse(Page.UpdateDate.ToString("r"))

  • Pavel Gurecki 55 posts 158 karma points
    Mar 29, 2013 @ 08:54
    Pavel Gurecki
    0

    Hi Curetos,

    thanks for your reply.

    Your solution should do the trick, I will try it out, but this is more like workaround for now.

     If only Umbraco would expose "Last published" date this task would be trivial. We should make a feature request for that if it is not implemented yet.

Please Sign in or register to post replies

Write your reply to:

Draft