as far as i know there isn't a published date set when an item is publiushed, you could in theory go through the db and find them, but that would probibly involve hitting the database quite a bit and slow everything down.
Instead you could create your own publish date property and then set it on the first publish of an item.
For the code below you can create the "firstPublished" property on any doctype the same way as any other property, and then some code can set the value on publish.
if you put something like below in app_code
using System;
using Umbraco.Core;
using Umbraco.Core.Services;
namespace UmbracoPackages.App_Code
{
public class PublishDate : ApplicationEventHandler
{
protected override void ApplicationStarted(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)
{
foreach(var item in e.PublishedEntities)
{
if (item.HasProperty("firstPublished"))
{
var pubDate = item.GetValue<DateTime>("firstPublished");
if (pubDate != null && pubDate != DateTime.MinValue)
{
item.SetValue("firstPublished", DateTime.Now);
ApplicationContext.Current.Services.ContentService.SaveAndPublishWithStatus(item);
}
}
}
}
}
}
I haven't actually tested this, but the theory is.
The code above will get called when any content is published. If the publishing item has a PublishedDate property and it's not set, set it to now, and then publish it.
There may be a need to cancel the publishing event, when you change something, but i am not sure.
from your front end razor you then have a FirstPublished property that you can sort anything by
Sort by first publish date
Hi,
I am wondering if anybody knows how to sort sub pages by first published date. Thus neither createDate nor updateDate.
Scott
Hi
as far as i know there isn't a published date set when an item is publiushed, you could in theory go through the db and find them, but that would probibly involve hitting the database quite a bit and slow everything down.
Instead you could create your own publish date property and then set it on the first publish of an item.
For the code below you can create the "firstPublished" property on any doctype the same way as any other property, and then some code can set the value on publish.
if you put something like below in app_code
I haven't actually tested this, but the theory is.
The code above will get called when any content is published. If the publishing item has a PublishedDate property and it's not set, set it to now, and then publish it.
There may be a need to cancel the publishing event, when you change something, but i am not sure.
from your front end razor you then have a FirstPublished property that you can sort anything by
or some such order query depending on what you are doing..
Kevin
Hi Kevin,
I catch the idea, I'll give it a look and see if it works as expected.
Seems like it'll do just the job.
Thanks!
is working on a reply...