13

I am working in sitecore version 7.2 and I was wondering how to get a last publish date for an item. I get that any item has date created and date updated but it does not have a property called date published. So, in order to get a last published date for an item, is it safe to assume that the last updated date in the web database for that particular item is the date the item was last published? Or is there something more elegant?

Please let me know.

Thanks!

9

You need to create some custom code to have such a functionality, out of the box Sitecore doesn't have such a functionality.

You can create a template with two fields : Publishing Date and Publishing By.

Create your own class:

 public class UpdatePublishingStatistics : PublishItemProcessor
 {
    private const string PublishedFieldName = "__Published";
    private const string PublishedByFieldName = "__Published By";

    public override void Process(PublishItemContext context)
    {
        SetPublishingStatisticsFields(context);
    }

    private void SetPublishingStatisticsFields(PublishItemContext context)
    {
        Assert.ArgumentNotNull(context, "context");
        Assert.ArgumentNotNull(context.PublishOptions, "context.PublishOptions");
        Assert.ArgumentNotNull(context.PublishOptions.SourceDatabase, "context.PublishOptions.SourceDatabase");
        Assert.ArgumentNotNull(context.PublishOptions.TargetDatabase, "context.PublishOptions.TargetDatabase");
        Assert.ArgumentCondition(!ID.IsNullOrEmpty(context.ItemId), "context.ItemId", "context.ItemId must be set!");
        Assert.ArgumentNotNull(context.User, "context.User");

        SetPublishingStatisticsFields(context.PublishOptions.SourceDatabase, context.ItemId, context.User.Name);
        SetPublishingStatisticsFields(context.PublishOptions.TargetDatabase, context.ItemId, context.User.Name);
    }

    private void SetPublishingStatisticsFields(Database database, ID itemId, string userName)
    {
        Assert.ArgumentNotNull(database, "database");
        Item item = TryGetItem(database, itemId);

        if (HasPublishingStatisticsFields(item))
        {
            SetPublishingStatisticsFields(item, DateUtil.IsoNow, userName);
        }
    }

    private void SetPublishingStatisticsFields(Item item, string isoDateTime, string userName)
    {
        Assert.ArgumentNotNull(item, "item");
        Assert.ArgumentNotNullOrEmpty(isoDateTime, "isoDateTime");
        Assert.ArgumentNotNullOrEmpty(userName, "userName");

        using (new SecurityDisabler())
        {
            item.Editing.BeginEdit();
            item.Fields[PublishedFieldName].Value = DateUtil.IsoNow;
            item.Fields[PublishedByFieldName].Value = userName;
            item.Editing.EndEdit();
        }
    }

    private Item TryGetItem(Database database, ID itemId)
    {
        try
        {
            return database.Items[itemId];
        }
        catch (Exception ex)
        {
            Log.Error(this.ToString(), ex, this);
        }

        return null;
    }

    private static bool HasPublishingStatisticsFields(Item item)
    {
        Assert.ArgumentNotNull(item, "item");
        return item.Fields[PublishedFieldName] != null
                && item.Fields[PublishedByFieldName] != null;
    }
}

Add your pipeline :

 <?xml version="1.0" encoding="utf-8"?>
 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   <sitecore>
     <pipelines>
      <publishItem>
       <processor type="Sitecore.Sandbox.Pipelines.Publishing.UpdatePublishingStatistics, Sitecore.Sandbox"                     patch:after="processor[@type='Sitecore.Publishing.Pipelines.PublishItem.UpdateStatistics, Sitecore.Kernel']" />
      </publishItem>
     </pipelines>
   </sitecore>
 </configuration>

All the informations you can find here :

https://sitecorejunkie.com/2013/01/26/who-just-published-that-log-publishing-statistics-in-the-sitecore-client/

  • Thanks a lot for such a detailed explanation! I will definitely try that out! Thanks again :) – Akshay Mahajan Oct 26 '16 at 13:16
  • 1
    I just copy/paste from that blogpost. Thanks to @SitecoreJunkie – Vlad Iobagiu Oct 26 '16 at 13:17
  • @SitecoreJunkie - Thanks a lot to you too! – Akshay Mahajan Oct 26 '16 at 13:19
0

You cannot get that information out of the box. What you could do, is implement a bit of custom code to get the information you are looking for. While using code, the following can help you get your result:

Database.Properties.GetLastPublishDate(Database target, Language language)

From here you can decide what to do with it.

  • Thanks Dilyano for getting back. But wouldn't the code you have mentioned above give me the last publish date for a specific database?, which may or may not be the last publish date for a specific item. However, thanks a lot for the pointer :) – Akshay Mahajan Oct 26 '16 at 13:11
0

To add to the answers for the sake of googling people of the future, in case the last date the item was saved will do for your purposes, you can use Sitecore.Context.Item.Statistics.Updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.