gpt4 book ai didi

c# - 如何以编程方式发布已使用编辑器更新的 Sitecore 项目

转载 作者:太空狗 更新时间:2023-10-29 22:36:21 25 4
gpt4 key购买 nike

我一直在尝试通过已在 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: )

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

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

最佳答案

这就是我过去在 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);
}
}

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

有时,当我在 master 数据库上以编程方式操作项目时,我强制 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);
}

关于c# - 如何以编程方式发布已使用编辑器更新的 Sitecore 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37432502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com