【问题标题】:Umbraco 7 - How do I get the published date from IPublishedContent?Umbraco 7 - 如何从 IPublishedContent 获取发布日期?
【发布时间】:2023-04-07 17:34:01
【问题描述】:

我找不到发布日期。我正在使用 Umbraco.Core.Models.IPublishedContent 接口,该接口似乎没有发布日期,只有创建和更新日期。

我在互联网上找到的所有文档都建议使用 Document(id),然后是 Document.ReleasedDate,但现在已标记为已过时。它建议在 Umbraco.Core.Models.Content 类中使用 ReleaseDate。

我错过了什么?

【问题讨论】:

    标签: c# asp.net-mvc-4 content-management-system umbraco


    【解决方案1】:

    在 IPublishedContent 上使用 UpdateDate。当您发布内容时,该日期始终会更新。

    您提到的 ReleaseDate 用于设置(自动)发布特定内容项的未来日期和时间。所以那不是你要找的日期。设置发布日期后,UpdateDate 也将在项目发布后更新为该日期。

    【讨论】:

    • 如果我使用UpdateDate,并发表一篇文章,然后三天后返回以修正拼写错误,现在“发表日期”将关闭三天。这是正确的吗?
    • 是的,没错。如果这是您的情况,最好将“发布日期”属性添加到您的内容中。然后,您可以选择通过事件处理程序设置日期,这可确保日期仅在首次发布时更新,或者只是将其作为必填字段,内容编辑者可以选择他们想要的任何日期。
    • 是的,我想我必须添加一个“发布日期”属性并创建一个事件处理程序。
    • 您能否指出如何设置此事件处理程序以及在发布文章时挂钩的方向?我是 Umbraco 的新手。
    【解决方案2】:

    Umbraco 内容项没有内置属性来指示它们的首次发布时间。

    如果您想获得内容实际发布时间的可靠指示,最好的选择是为您的文档类型添加自定义属性。然后,您可以向您的应用程序添加一个事件处理程序,它将属性更新为首次发布时的当前日期:

    using System;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Services;
    
    namespace YourNamespace
    {
        /// <summary>
        /// Updates the publishedDate property when content is first published
        /// </summary>
        public class UpdatePublishDateEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Published += ContentService_Published;
            }
    
            void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
            {
                const string publishedDateKey = "publishedDate";
                var contentService = ApplicationContext.Current.Services.ContentService;
                foreach (var content in e.PublishedEntities.Where(x => x.HasProperty(publishedDateKey)))
                {
                    var existingValue = content.GetValue(publishedDateKey);
                    if (existingValue == null)
                    {
                        content.SetValue(publishedDateKey, DateTime.Now);
                        contentService.SaveAndPublishWithStatus(content, raiseEvents: false);
                    }
                }
            }
        }
    }
    

    Umbraco 在启动时会自动扫描并激活继承自 ApplicationEventHandler 的类,因此您只需将上述类添加到您的项目中即可。

    【讨论】:

      【解决方案3】:

      如果您使用的是 Umbraco 7,请查看 Umbraco.Core.Models.IContent 接口上的 ReleaseDate 属性。显然它“获取或设置内容应该发布并因此发布的日期”。

      【讨论】:

      • 你上面的回复中已经报告了。这不是 pwee 所寻找的,因为该日期在每次更改后都会重置。
      • 我自己进行了测试,发现在后台标记为“Publish at”的“ReleaseDate”在进行修改(例如拼写更正)后不会改变)。所以我可以根据需要使用它。谢谢@Teppic。
      【解决方案4】:

      我认为您可能应该使用“.Created”日期。因为这将是文章最初发表的日期。

      或者,您可以在 DocType 上使用自定义 DateTime 属性,并将其用作发布日期,方法是按如下方式检索它:

      YourNodeObject.GetPropertyValue<DateTime>("customPropertyAliasHere");
      

      问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-25
        • 1970-01-01
        相关资源
        最近更新 更多