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
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.
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.
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.
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.
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?
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
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.
You can use the CreateDate property. This property defines the date and time that the content created and it is not going to change.
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
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
I just wrote a class which will make what you want.
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.
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/
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.
Thank you! It works!
To help other people, this is how you can get items ordered by publishDate:
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?
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/
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.
This worked for me:
is working on a reply...