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();
}
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Powershell Concatenate String

PowerShell Concatenate String

Table of Contents Hide Introduction to PowerShell Concatenate StringHow to Concatenate Strings and Variables in PowerShell#1. Expanding Strings#2. Substitution or string.Format approach#3 Join Operator Microsft PowerShell scripting is little different from…
View Post