Sitecore Publish Queue

The Sitecore publish queue contains the historical and current items that needs to be published. Every time an item is ready to be published (when you save changes, or the item becomes publishable from a workflow) it is added to the queue, ensuring that items are added or removed from the website in the correct order.

This mechanism allows Sitecore to do incremental publishings. Only publishing the new or changed items speeds up the publish.

Sitecore has no tool for seing this queue (except for this example for Sitecore 5.1), but that does not mean that you cannot make a tool yourself.

I have here 2 ways of seing the queue.

The Historical View

Sitecore provides a PublishManager that gives acces to various publishing infos. Among these are the GetPublishQueue function that allows you to see whats inside the queue within a start and an end date. The view is only interesting if you wish to see what items has been published, it will not give you the current items that will be publsihed when selecting “Incremental Publish”:

// Get the master and the web database
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master"); 
Sitecore.Data.Database web = Sitecore.Data.Database.GetDatabase("web");

// Get all ID's in the publish queue
Sitecore.Collections.IdList list = Sitecore.Publishing.PublishManager.GetPublishQueue(DateTime.MinValue, DateTime.MaxValue, 

foreach (Sitecore.Data.ID id in list)
{
  // Try to get the item from the master database
  Sitecore.Data.Items.Item item = master.GetItem(id);
  if (item == null)
  {
    // The item is not found in the master database, it means that the item
    // must be deleted. See if you can find it in the web database.
    // If you can, it's an item to be deleted. If it's not found
    // it has already been deleted from the web database, and the publish
    // is an old one.
    Sitecore.Data.Items.Item toBeDeleted = web.GetItem(id);
    if (toBeDeleted != null)
      Response.Write("<font color='red'>" + toBeDeleted.Paths.FullPath + "</font><br/>");
    else
      Response.Write("<font color='red'>" + id.ToString() + "</font><br/>");     
  }
  else
  {
    // The item was found in the master database. It's an item that was published
    Response.Write(item.Paths.FullPath + "<br/>");
  }
} 

The current view

This view is way more interesting, as it shows you which items will be published the next time you select “Incremental Publish”. Again, items being added, changed or deleted are all present. Sitecore uses a Pipeline to do this job. That’s why the functions are found in the Sitecore.Publishing.Pipelines namespace:

// Get the master and the web databases
Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master");
Sitecore.Data.Database web = Sitecore.Data.Database.GetDatabase("web");
   
// Create some publishing options.
// Only the Language parameter is used by the pipeline.
Sitecore.Publishing.PublishOptions options = new Sitecore.Publishing.PublishOptions(master, web, Sitecore.Publishing.PublishMode.Incremental, Sitecore.Globalization.Language.Parse("da"), DateTime.Now);

// Get all the publishing candidates
System.Collections.Generic.IEnumerable<Sitecore.Publishing.Pipelines.Publish.PublishingCandidate> candidates = Sitecore.Publishing.Pipelines.Publish.PublishQueue.GetPublishQueue(options);
foreach (Sitecore.Publishing.Pipelines.Publish.PublishingCandidate candidate in candidates)
{
  // Try finding the item in the master database
  Sitecore.Data.Items.Item item = master.GetItem(candidate.ItemId);
  if (item == null)
  {
    // The item was not found in the master database. This means that the
    // item is about to be deleted. Get the item from the web database instead 
    Sitecore.Data.Items.Item toBeDeleted = web.GetItem(candidate.ItemId);
    if (toBeDeleted != null)
      Response.Write("<font color='red'>" + toBeDeleted.Paths.FullPath + "</font><br/>");
    else
      Response.Write("<font color='red'>" + candidate.ItemId.ToString() + "</font><br/>");
  }
  else
  {
    // The item was found in the master database, meaning that it is about to be
    // published.
    Response.Write(item.Paths.FullPath + "<br/>");
  }
}

After notes:

While testing these code snippets I observed the following:

  • Items are added to the publish queue immediately after being created (unless you are using workflows)
  • If you publish a single item, the item is not removed from the publish queue. So if you do an incremental publish just after a single item publish, the item is published twice.

 

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Sitecore 6 and tagged , , , , , . Bookmark the permalink.

3 Responses to Sitecore Publish Queue

  1. Shawn says:

    Problem with this line.

    // Get all ID’s in the publish queue
    Sitecore.Collections.IdList list = Sitecore.Publishing.PublishManager.GetPublishQueue(DateTime.MinValue, DateTime.MaxValue,

    Like

  2. Pingback: Sitecore Publish Queue and Incremental Publish | Sean Holmesby

  3. Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.