Publish items in Sitecore programmatically using API

In Some scenarios if you would like to publish items in Sitecore programmatically using API from Master DB to web or pub Database then we can use the Sitecore Publishing API.

Sitecore has different types of publishing like Incremental, Full Publish, Single Item, Smart Publish. Use PublishMode in the publish options method to specify the type of the publish you need to achieve. If you choose Incremental publish mechanism you cannot set a root item as all items in the publish queue is published. Below is the sample code to publish an item.

Sample Code to  publish items in Sitecore programmatically using API

private void PublishItem(Sitecore.Data.Items.Item item)
{
  // The publishOptions determine the source and target database,
  // the publish mode and language, and the publish date
  Sitecore.Publishing.PublishOptions publishOptions =
    new Sitecore.Publishing.PublishOptions(item.Database,
                                           Database.GetDatabase("web"),
                                           Sitecore.Publishing.PublishMode.SingleItem,
                                           item.Language,
                                           System.DateTime.Now);  // Create a publisher with the publishoptions
  Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions);
 
  // Choose where to publish from
  publisher.Options.RootItem = item;
 
  // Publish children as well?
  publisher.Options.Deep = true;
 
  // Do the publish!
  publisher.Publish();
}

0 Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like
XOR in Python

XOR in Python

XOR Operator in Python is also known as “exclusive or”  that compares two binary numbers bitwise if two bits are…
How To

How to configure Git proxy?

If you are working in the corporate company all the internet requests usually goes through the corporate firewall.…