Publish specific version of Sitecore item

Posted 22 May 2019 by Marek Musielak

publish selected version of sitecore item

When Sitecore publishes an item, it always publishes the most recent valid version of that item. It checks workflow state, publishing restrictions, item validity dates and newer versions. But what if we want to publish a version of an item which has no publishing restrictions but there are newer versions of that item?

If you select an earlier version of your item in Sitecore Content Editor you will see a warning saying that newer version will be published instead:

sitecore publish version warning

So how can we publish older version of an item? One option would be to make newer versions not publishable, another option is changing validity dates for newer versions or move them to any non-final workflow state. But what if we don't want to make unnecessary changes to the item? You may be surprised, but Sitecore API allows to publish a version of an item even if there are newer versions.

So how can we do this? With PublishOptions object and its PublishDate property. PublishDate tells Sitecore what is the date that should be used to find which version of an item was valid at given time. We can pass the date when the chosen version was created - at that time there were no newer versions, so Sitecore will publish the version we're interested in instead of publishing the newest version. Here is a sample code:

public PublishItemResult PublishSelectedVersion(Item version, Database targetDatabase)
{
  var options = new PublishOptions(
    version.Database, 
    targetDatabase, 
    PublishMode.Full, 
    version.Language, 
    version.Statistics.Created);
    
  var context = PublishManager.CreatePublishItemContext(version.ID, options);
  context.PublishContext = new PublishContext(options);
  return PublishItemPipeline.Run(context);
}

And simple code showing how to use method above:

int versionNumber = 1;
var version = Version.Parse(versionNumber);
var itemUri = new ItemUri(itemId, language, version, "master");
var itemVersion = Database.GetItem(itemUri);
var targetDatabase = Database.GetDatabase("web");
var result = PublishSelectedVersion(itemVersion, targetDatabase);

Whichever version number you will choose, Sitecore will use that version's version.Statistics.Created date to make sure that your chosen version is published instead of the most recent version.

Remember:

  • Version which you want to publish cannot have publishing restrictions set for the time it was created.
  • Version which you want to publish cannot be in not-final workflow state.
  • Use version.Statistics.Created instead of just version.Created - the latter is the item creation date, not the version.
  • When you publish this item again without using PublishSelectedVersion method, it will publish the latest valid version of the item.

If you have any comments use the links below to get in touch. Thank you for reading. You can find more Sitecore related articles here.

Comments? Find me on or Sitecore Chat