【问题标题】:How to publish Sitecore items programatically that have been updated using the editor如何以编程方式发布已使用编辑器更新的 Sitecore 项目
【发布时间】:2016-05-25 09:08:38
【问题描述】:

我一直在尝试通过 Sitecore 编辑器中已更改的代码发布项目。如果我以编程方式更新字段值并发布这些更改,那么它可以正常工作。

我们的内容管理编辑器会定期在编辑器中进行更改,而不必发布它们。我们希望为他们提供一种功能,即单击一个按钮即可发布所有相关更改并清除 Sitecore 缓存。

我不想发布整个网站,只发布几个预定义的项目。

我们目前使用的是 Sitecore.NET 6.4.1(修订版 110720)。我无法更新 Sitecore。

我尝试了以下选项:

选项 1:实例化一个新的发布者对象

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(); 

选项 2:使用静态发布管理器

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);

这两种方法都包含在 using 语句中,以使用内容管理编辑器使用的同一帐户。

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 ...
      }

}

据我所知,日志没有显示任何值得注意的内容

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: )

这绝对不是缓存问题,因为在以编程方式清除缓存之前在编辑器中手动发布时,更改在代码中可见。

所以我正在寻找一种以编程方式发布更新项目的预定义列表的方法,而不管编辑的方式。

【问题讨论】:

  • 在您的选项 2 (publishmanager) 中,您将发布到主数据库。您可以尝试以 Web 数据库为目标的那个吗?另外,请确保您使用的是正确的数据库上下文(也可以使用数据库切换器来主数据库)。
  • 似乎也不起作用。我添加了一个 DatabaseSwitcher 并将数据库更改为 web,也尝试了另一种方式。
  • 我使用选项2,你能用网络更新问题中的代码吗?,其他发布目标并不总是“网络”,最好不要使用这个硬编码,2)如果父不存在在网络数据库中,该项目不会被发布
  • 已更新为“web”,这不是问题,因为我尝试了两者来确定,但我想我想通了,稍后会发布答案

标签: c# .net sitecore sitecore6


【解决方案1】:

这就是我过去在 Sitecore 6.5 解决方案中发布项目的方式。看起来与您的第一个解决方案非常相似,这对我来说仍然很好。您是否在 Sitecore 日志中看到任何错误/信息日志?

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);
    }
}

关于缓存,应该由网络数据库上的发布操作自动处理。

有时,当我在主数据库上以编程方式操作项目时,我会强制 Sitecore 仅使用以下代码清除那些项目的缓存:

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);
}

【讨论】:

  • 我尝试使用您的代码,但它与我的第一个选项基本相同,因此结果没有差异。
  • 您在日志中看到什么了吗?
  • 从日志看来,一切正常,推送了 2 个项目并清除了缓存。那有什么问题呢?您的网站是否指向网络数据库,或者您在哪里检查这些更改?
  • 我正在检查 dbbrowser,以便可以轻松地在 master/web db 之间切换。我还在检查用于从 Sitecore 获取数据的自定义 Web 服务。
  • 尝试使用 /sitecore/admin/cache.aspx 清除缓存,以确保它不是背后的一些缓存。看来出版进展顺利。
【解决方案2】:

毕竟,问题不在于代码,而在于错误的执行顺序和使用正确的数据库。

  1. 更新 Sitecore 编辑器中的字段
  2. 清除主数据库缓存
  3. 从主数据库中显式获取项目
  4. 以“网络”数据库为目标发布项目
  5. 清除“网络”数据库缓存

发布项目的最终代码:

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);
}

清除缓存的代码:

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

【讨论】:

  • 我试过你的代码,它运行良好。就我个人而言,我不使用您的 clearcache 函数,因为我已经在我的代码中发布了 item 对象。无需在主数据库中搜索。
【解决方案3】:

据我所知,在发布时清除所有缓存不是一种实用的方法。它会对网站性能产生很大影响。

实际上,从 dbbrowser 编辑内容后,您只需清除项目特定缓存(仅适用于已编辑项目)。要知道如何做到这一点,请查看 - http://sitecoreblog.patelyogesh.in/2013/08/sitecore-partial-cache-clear-programmatically.html

意味着,您应该采取的步骤: 1. 从 DBBrowser 编辑内容 2.发布前,为发布项目清除项目特定缓存 3. 发布时无需额外的“网络”或“主”缓存清除。

我相信这会对您的解决方案有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    相关资源
    最近更新 更多