Santa

Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Sitecore Item Publish using Code (Programmatically)

August 18, 2022
Sitecore Item Publish using Code (Programmatically)
Keyur Garala
Keyur Garala
Sitecore Certified Architect
sitecore-item-publish-using-code-programmatically

When you create, edit or delete new items in Sitecore, they must be published before they appear on your website. This lets you save unfinished items and decide exactly when the items are to be launched on the website.

When you work in Sitecore, the items you have saved are saved in the Master database. When you want to publish the new items or updated items, they are copied from the Master database, to a publishing target and from that, the website is launched. The default publishing target is the web database.

Sitecore has different types of publishing like Incremental, Full Publish, Single Item, Smart Publish.

Below is the code we will be using for Sitecore item publish using code.

Publishing an Item Programmatically
                                
// 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(); 
                                
					        

We are following the above code in our custom logic where we need to publish the item using the code. But in one of the cases, we were facing strange issues. Items were getting published even when they were not there in the final workflow state. Refer to the scenario below for more clarification.

Let me show you the detailed structure.

  • item1 (published to prod workflow)
    • item2 (non workflow )
      • item3 (non workflow )
        • item4 (draft workflow)

My requirement is to update item one and after the update publish the same item to Web.

So as per the requirement, I was publishing tem1 after update using code and it will also publish all the child items. And if you publish the same item from CMS the item4 (draft workflow state) will not be published. But in my case, it gets published by mention code.

                                
PublishOptions po = new PublishOptions(database, databaseWeb, PublishMode.Smart, Sitecore.Context.Language, DateTime.Now); 

po.RootItem = item; 

po.Deep = true; // Publishing subitems 

(new Publisher(po)).Publish(); 
                                
                            
Solution:

So, to fix that issue I need to update the code as below:

                                
Database web = Factory.GetDatabase("web"); 

Sitecore.Publishing.PublishOptions po = new Sitecore.Publishing.PublishOptions(master, web, Sitecore.Publishing.PublishMode.SingleItem, Sitecore.Context.Language, DateTime.Now); 

po.Deep = false; 

po.PublishRelatedItems = true; 

po.RootItem = sitecoreRootItem; 

Sitecore.Publishing.PublishOptions[] pos = { po }; 

Sitecore.Publishing.PublishManager.Publish(pos);

                                
                            

YOU MAY ALSO LIKE