How to programmatically publish Sitecore items that have been updated using the editor

I am trying to publish elements through code that has been modified in the Sitecore editor. If I update the field values ​​programmatically and post these changes, it works fine.

Our content management editors regularly make changes to the editor without publishing them. We want to provide them with the functionality to click one button, which publishes all the relevant changes and clears the Sitecore cache.

I do not want to publish the entire site, only a few predefined elements.

We are currently using Sitecore.NET 6.4.1 (version 110720). I have no way to update Sitecore.

I tried these options:

Option 1: Create an Instance of a New Publisher Object

Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Database web = Sitecore.Configuration.Factory.GetDatabase("web"); Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(master, web, Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Deep = true; publisher.Publish(); 

Option 2: use a static publication editor

 Database db = Sitecore.Configuration.Factory.GetDatabase("web"); Database[] databases = new Database[1] { db }; Sitecore.Handle publishHandle = Sitecore.Publishing.PublishManager.PublishItem(item, databases, db.Languages, true, false); 

Both methods are enclosed in a using statement to use the same account that is used by content management editors.

 string domainUser = @"sitecore\admin"; if (Sitecore.Security.Accounts.User.Exists(domainUser)) { Sitecore.Security.Accounts.User user = Sitecore.Security.Accounts.User.FromName(domainUser, false); using (new Sitecore.Security.Accounts.UserSwitcher(user)) { // publish code ... } } 

Magazines show nothing remarkable as far as I can tell

 ManagedPoolThread #7 13:41:46 INFO Job started: Publish to 'web' ManagedPoolThread #7 13:41:46 INFO HtmlCacheClearer clearing HTML caches for all sites (5). ManagedPoolThread #7 13:41:46 INFO HtmlCacheClearer done. ManagedPoolThread #7 13:41:46 INFO Job ended: Publish to 'web' (units processed: 2) ManagedPoolThread #5 13:41:46 INFO Job ended: Publish (units processed: ) 

This is definitely not a caching problem, because when manually published in the editor before clearing the cache programmatically, the changes are visible in the code.

So, I'm looking for a way to programmatically publish a predefined list of updated items, ignoring the editing method.

+5
source share
3 answers

So I published the elements in the Sitecore 6.5 solution. It looks very similar to your first decision, and it still works well for me. Do you see any errors / informational magazines in Sitecore magazine?

 public void PublishItem(Database dbMaster, Database dbWeb, Item iParent) { try { PublishOptions po = new PublishOptions(dbMaster, dbWeb, PublishMode.SingleItem, Sitecore.Context.Language, DateTime.Now); po.RootItem = iParent; po.Deep = true; // Publishing subitems (new Publisher(po)).Publish(); } catch (Exception ex) { Sitecore.Diagnostics.Log.Error("Exception publishing items from custom pipeline! : " + ex, this); } } 

As for caching, it should be handled by the publish operation automatically in the web database.

Sometimes, when I program elements in the main database programmatically, I force Sitecore to clear the cache only for elements with the code below:

 Item baseitem = Database.GetDatabase("master").SelectSingleItem(sitecore_path); if (baseitem != null) { //Sitecore.Caching.CacheManager.ClearAllCaches(); string load = String.Concat(new object[] { "item:load(id=", baseitem.ID, ",language=", baseitem.Language, ",version=", baseitem.Version, ")" }); Sitecore.Context.ClientPage.SendMessage(this, load); String refresh = String.Format("item:refreshchildren(id={0})", baseitem.Parent.ID); Sitecore.Context.ClientPage.ClientResponse.Timer(refresh, 2); } 
+4
source

According to my words, clearing all caches when publishing is not a practical way. This will greatly affect site performance.

Actually, after editing the content from dbbrowser, you just need to clear the specific cache (only for edited items). To learn how to do this, check out http://sitecoreblog.patelyogesh.in/2013/08/sitecore-partial-cache-clear-programmatically.html .

So, the steps you must take are: 1. Edit the contents from DBBrowser 2. Before publishing, explicit cache code for publishing items 3. There is no need to further clear the "web" or "master" cache.

I am sure this will help your decision.

+2
source

In the end, the problem was not in the code itself, but in the wrong order of actions and the use of the correct databases.

  • Refresh the field in the Sitecore editor
  • Clear primary database cache
  • Get item explicitly from main database
  • Publish the item in the web database as the target
  • Clear web database cache

Final code for posting the item:

 using (new Sitecore.SecurityModel.SecurityDisabler()) { string itemId = "E7A2DE22-4338-49A6-840F-4B3124F1FFBD"; Database webdb = Sitecore.Configuration.Factory.GetDatabase("web"); Database masterdb = Sitecore.Configuration.Factory.GetDatabase("master"); ClearSitecoreDatabaseCache(masterdb); Item masterItem = masterdb.GetItem(new ID(itemId)); // target databases Database[] databases = new Database[1] { webdb }; Sitecore.Handle publishHandle = Sitecore.Publishing.PublishManager.PublishItem(masterItem, databases, webdb.Languages, true, false); ClearSitecoreDatabaseCache(webdb); } 

Code to clear the cache:

 public void ClearSitecoreDatabaseCache(Database db) { // clear html cache Sitecore.Context.Site.Caches.HtmlCache.Clear(); db.Caches.ItemCache.Clear(); db.Caches.DataCache.Clear(); //Clear prefetch cache foreach (var cache in Sitecore.Caching.CacheManager.GetAllCaches()) { if (cache.Name.Contains(string.Format("Prefetch data({0})", db.Name))) { cache.Clear(); } } } 
+1
source

All Articles