Sitecore have two entries to publishing:
- Create a new Publisher and call Publish() or PublishAsync()
- Use the Sitecore.Publishing.PublishManager
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:
- Sitecore publish certain items on save by briancaos
- Create and publish items in Sitecore by briancaos
- Sitecore Publishing Service from Sitecore
- Sitecore manual publish an item in all publishing targets and languages by tools4geeks
Pingback: Sitecore publish certain items on save | Brian Pedersen's Sitecore and .NET Blog
Pingback: Create and publish items in Sitecore | Brian Pedersen's Sitecore and .NET Blog
Pingback: Sitecore Publish item when moved or dragged using uiMoveItems and uiDragItemTo pipelines | Brian Pedersen's Sitecore and .NET Blog
Pingback: Sitecore publishItem pipeline – handling missing delete when publishing | Brian Pedersen's Sitecore and .NET Blog