It indeed seems that Page.LastPublishedDate
is never set by DD4T Templates. DXA (currently) also has no OOTB model mapping for this property.
However, it is quite easy to obtain the last publish date using the CD API. Something like:
PageMetaFactory pageMetaFactory = new PageMetaFactory(Convert.ToInt32(localization.LocalizationId));
IPageMeta pageMeta = pageMetaFactory.GetMeta(Convert.ToInt32(pageModel.Id));
DateTime lastPublishDate = pageMeta.LastPublicationDate;
There is no need to use the DD4T Provider for this purpose.
If you want all your Page Models to have a last publish date set, you could create your own Page Controller: a subclass of DXA's built-in PageController
which overrides the EnrichModel
method. You can put above logic in that override. You could store the date in PageModel.ExtensionData
:
using Sdl.Web.Mvc.Controllers;
using System;
using Sdl.Web.Common.Models;
namespace Example
{
public class MyPageController : PageController
{
protected override ViewModel EnrichModel(ViewModel model)
{
PageModel pageModel = (PageModel) base.EnrichModel(model);
Localization localization = WebRequestContext.Localization;
PageMetaFactory pageMetaFactory = new PageMetaFactory(Convert.ToInt32(localization.LocalizationId));
IPageMeta pageMeta = pageMetaFactory.GetMeta(Convert.ToInt32(pageModel.Id));
pageModel.SetExtensionData("LastPublishDate", pageMeta.LastPublicationDate);
return pageModel;
}
}
}
PageMetaFactory
because ofinternal class PublicationResolver : IPublicationResolver
inSdl.Web.Tridion.Mapping
namespace.PageMetaFactory
.