Sitecore Publish Items using the PublishManager

Sitecore have two entries to publishing:

You should use the PublishManager. Sitecore introduced the Publishing Service which is an optional external publisher that is much faster than the built in publishing. It comes with a small price as there are differences to integration points, and publishing behavior. But if you use the PublishManager you will be ready to switch in case you need the improved performance and are willing to manage yet another external service.

Using the Publisher class will only publish through the built in publisher.

The PublishManager is very simple to use:

using Sitecore.Data.Items;
using System;
using Sitecore.Data;
using Sitecore.Publishing;

PublishOptions publishOptions = new PublishOptions(item.Database, Database.GetDatabase("web"), publishMode, item.Language, DateTime.Now);
publishOptions.RootItem = item;
publishOptions.Deep = deepPublish;
publishOptions.PublishRelatedItems = publishRelatedItems;
publishOptions.CompareRevisions = compareRevisions;

var handle = PublishManager.Publish(new PublishOptions[] { publishOptions });
PublishManager.WaitFor(handle);

The WaitFor() call is only needed if you wish to wait for the publish to end before continuing.

You can create a simple extension method that can publish an item like this:

using Sitecore.Data.Items;
using System;
using System.Globalization;
using Sitecore.Data;
using Sitecore.Diagnostics;
using Sitecore.Publishing;

namespace MyCode
{
  public static class ItemExtensions
  {
    public static void PublishItem(this Item item, PublishMode publishMode, bool publishAsync = false, bool deepPublish = false, bool publishRelatedItems = false, bool compareRevisions = false)
    {
      
      if (item == null)
        return;

      PublishOptions publishOptions = new PublishOptions(item.Database, Database.GetDatabase("web"), publishMode, item.Language, DateTime.Now);
      publishOptions.RootItem = item;
      publishOptions.Deep = deepPublish;
      publishOptions.PublishRelatedItems = publishRelatedItems;
      publishOptions.CompareRevisions = compareRevisions;

      var handle = PublishManager.Publish(new PublishOptions[] { publishOptions });
      if (publishAsync)
        return;
      PublishManager.WaitFor(handle);
    }
  }
}

And you can use the extension method like this:

Item dbItem = Context.ContentDatabase.GetItem(xxx,xxx,xxx);
dbItem.PublishItem(PublishMode.SingleItem, false, false, false, false);

Thanks to Stephen Pope for reminding me of the existence of the PublishingManager class.

MORE TO READ:

Advertisements

About briancaos

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

2 Responses to Sitecore Publish Items using the PublishManager

  1. Pingback: Sitecore publish certain items on save | Brian Pedersen's Sitecore and .NET Blog

  2. Pingback: Create and publish items in Sitecore | Brian Pedersen's Sitecore and .NET Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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