Compare and Contrast Ways to Programmatically Publish Sitecore Items

newspaper kitty by Brittany Randolph, on FlickrWhen researching how to publish Sitecore items in code, anyone that has done a quick search around the web can tell you that there are many different approaches. Even a quick scan of Sitecore objects will reveal a lot of publishing related classes and methods. This produces a common problem: too many options, but not enough information about which way is “best” or most appropriate for your situation. That’s why I decided to write this blog post. Hopefully this will help you get a better grasp of the different ways to trigger an item publish programmatically, and how to choose the best method for your application.

Option 1: Instantiate and use a new “Publisher” object

Sitecore.Publishing.PublishOptions publishOptions =
  new Sitecore.Publishing.PublishOptions(item.Database,
                                         Database.GetDatabase("web"),
                                         Sitecore.Publishing.PublishMode.SingleItem,
                                         item.Language,
                                         System.DateTime.Now);
Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions);
publisher.Options.RootItem = item;
publisher.Options.Deep = true;
var publishResult = publisher.PublishWithResult();

Benefits:

  • Publish occurs immediately.
  • Receive feedback when publishing is completed to know whether or not it was successful, and the details of the action.

Drawbacks and caveats:

  • Does not use the Job Manager, so you cannot inspect the publishing status in the Job Viewer while it is running.
  • Does not wait for other publish operations to complete, so it is possible to introduce timing-related issues

Scenario:

If you have code that is creating or editing items in the master database, and those items need to be immediately published and available on the website, then you might consider using this method.

Option B: Trigger the PublishPipeline

PublishContext publishContext = PublishManager.CreatePublishContext(publishOptions);
var publishResult = PublishPipeline.Run(publishContext);

Benefits:

  • Fast, since it runs the pipeline directly (but only barely faster than using the Publisher class)

Drawbacks and caveats:

  • More complex to use
  • Does not report progress without manually harnessing a Job for it to run under.
  • Circumvents most Sitecore configuration checks

Scenario:

I would not recommend calling the publish pipeline directly. This is deeper into the code, so it is more likely to break with future Sitecore versions, and it doesn’t save you very much at all over using one of the higher-level objects.

Option III: Manually add items to the publishing queue

Sitecore.Publishing.PublishManager.AddToPublishQueue(item, ItemUpdateType.Created);

Benefits:

  • Simply adds an entry into the PublishQueue table, so it is super fast.
  • Highly scalable, since adding to the queue is crazy fast, and duplicate entries are ignored by incremental publish, so you don’t have to worry about the same item getting published 15 times in one operation.

Drawbacks and caveats:

  • Doesn’t publish the item immediately, just adds it to the publish queue
  • Items in the publish queue will not be published unless someone manually triggers an incremental publish, or you have enabled the PublishAgent. This will result in a delay between when the item is edited, and when the changes appear on the site.

Scenario:

You have a high-demand system with a lot of content changes happening frequently. Maybe from some scheduled sync process, or even manually by a large team of content editors. In this case, I would recommend that you disable all manual publishing and enable the Sitecore PublishAgent. You can safely use this in conjunction with auto-publish that is triggered by workflow actions.

Option Green: Use the static PublishManager

Sitecore.Publishing.PublishManager.PublishSmart(master, targetDBs, languages);

Benefits:

  • This is the publishing method that is used by the Sitecore UI, so it closely mimics an actual user clicking the publish button in the Sitecore content editor.
  • Honors all configuration settings for whether or not publishing is enabled, what PublishStrategy is configured, etc.
  • Registers the action in the Sitecore audit log and runs the operation through the Sitecore JobManager.
  • This seems to be the most “Sitecore friendly” way to publish items.

Drawbacks and Caveats

  • Call is async and returns a job handle, so it returns quickly, but your code needs to monitor the job status if you have to know that the publish failed or succeeded.
  • Since this method does more things, it can be slightly slower to complete the actual publish action.

Scenario:

This is the method I recommend almost all of the time. This method is the friendliest to Sitecore and to your system admins, since it honors all of Sitecore’s configuration settings and restrictions. It is the simplest to use in code, since all of the lower-level parts are handled for you.

Final words

I would almost always recommend using the PublishManager if you need to publish items from code. It is the highest-level API method available for publishing, so it is least likely to break in future releases. I really appreciate how open the Sitecore codebase is, but this is one case where I have seen too many people abusing the lower-level Sitecore objects, when the cleaner high-level API would have been better.

Did I miss a publishing method you have used or seen? Let me know in the comments!

3 Responses to “Compare and Contrast Ways to Programmatically Publish Sitecore Items”

  1. James Skemp

    Hi Patrick.

    Nice article, but I have a question about Option Green. I dug around a bit since the code you included didn’t set an item to publish. From what I can tell this effectively does a site publish; is that correct?

    Thanks!

    Reply
    • Patrick

      That is true. The line of code here would trigger a smart site publish, but the PublishManager also has a method to `PublishItem`.

      Reply

Leave a Reply to Daniel Cancel reply