Quantcast
Channel: briancaos – Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 277

Sitecore Publish Items using the PublishManager

$
0
0

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:


Viewing all articles
Browse latest Browse all 277

Latest Images

Trending Articles



Latest Images