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)

author-icon-1 Keyur Garala

category-icon Sitecore Training

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 save are saved to the Master database. When you publish new items or updated items, the items are copied from the Master database to a publishing target, and from the publishing target, 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 code but in one of the cases, we face strange issues. Items are getting published even if it's not in the final workflow state. Please 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);

                                
                            

Recent Posts