Sitecore C#:如何从PublishEndRemoteEventArgs获取发布选项及远程发布信息?
解决Sitecore远程发布事件中获取发布上下文与关联项发布的问题
我来帮你梳理下在Sitecore的publish:end:remote事件中获取发布选项、目标,以及确保关联项发布到相同目标的具体实现方案:
1. 从PublishEndRemoteEventArgs中获取发布选项与目标
在远程发布事件处理器中,PublishEndRemoteEventArgs本身就携带了完整的发布上下文信息,你可以直接通过它的属性拿到关键数据:
using Sitecore.Publishing; using Sitecore.Publishing.Events; using Sitecore.Data.Items; using System.Collections.Generic; using System.Linq; public class PublishEventHandler { public void OnPublishEnd(object sender, PublishEndEventArgs args) { // 你已经实现的本地事件逻辑 HandlePublish(args.PublishOptions, args.Context.PublishItem); } public void OnPublishEndRemote(object sender, PublishEndRemoteEventArgs args) { // 从远程事件参数中获取发布选项 var publishOptions = args.PublishOptions; if (publishOptions == null) { Sitecore.Diagnostics.Log.Warn("No publish options found in remote publish end event", this); return; } // 获取触发发布的原项 var publishedItem = args.Context.PublishItem; if (publishedItem == null) { Sitecore.Diagnostics.Log.Warn("No published item found in remote publish end event", this); return; } // 复用本地事件的处理逻辑 HandlePublish(publishOptions, publishedItem); } private void HandlePublish(PublishOptions publishOptions, Item publishedItem) { // 检查是否是你需要处理的特定类型项 var targetTemplateId = new Sitecore.Data.ID("{YOUR-TARGET-TEMPLATE-ID}"); if (!publishedItem.TemplateID.Equals(targetTemplateId)) { return; } // 获取关联的特定项(这里替换成你的关联项查询逻辑) var relatedItems = GetRelatedItems(publishedItem); // 发布关联项到相同目标 foreach (var relatedItem in relatedItems) { // 复用原发布选项,仅修改根项为关联项 var newPublishOptions = new PublishOptions( publishOptions.SourceDatabase, publishOptions.TargetDatabase, publishOptions.Mode, relatedItem.Language, publishOptions.PublishDate ) { RootItem = relatedItem, Deep = false // 根据需求设置是否深度发布子项 }; var publisher = new Publisher(newPublishOptions); try { publisher.Publish(); Sitecore.Diagnostics.Log.Info($"Successfully published related item: {relatedItem.Paths.FullPath}", this); } catch (Exception ex) { Sitecore.Diagnostics.Log.Error($"Failed to publish related item: {relatedItem.Paths.FullPath}", ex, this); } } } private IEnumerable<Item> GetRelatedItems(Item sourceItem) { // 示例:从字段中获取关联项ID并加载项 var relatedItemIds = sourceItem.Fields["RelatedItems"].Value.Split('|') .Where(id => !string.IsNullOrEmpty(id)) .Select(id => new Sitecore.Data.ID(id)); return relatedItemIds.Select(id => sourceItem.Database.GetItem(id)) .Where(item => item != null); } }
2. 关键注意事项
- 复用发布选项:直接基于原事件的
PublishOptions创建新实例,确保TargetDatabase、PublishMode、Language等参数与原发布完全一致,这样关联项就会发布到相同的目标。 - 远程事件配置:确保你的处理器在Sitecore配置中正确注册了
publish:end:remote事件,示例配置:<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <events> <event name="publish:end"> <handler type="YourNamespace.PublishEventHandler, YourAssembly" method="OnPublishEnd"/> </event> <event name="publish:end:remote"> <handler type="YourNamespace.PublishEventHandler, YourAssembly" method="OnPublishEndRemote"/> </event> </events> </sitecore> </configuration> - 避免循环发布:如果关联项的发布也会触发
publish:end事件,一定要在HandlePublish方法中添加判断,防止无限循环(比如检查当前发布的项是否是关联项类型,直接跳过处理)。 - 日志与异常处理:添加详细的日志记录,方便排查发布过程中出现的问题,同时捕获异常避免影响主发布流程。
内容的提问来源于stack exchange,提问作者Erica Stockwell-Alpert
相关产品推荐
相关产品推荐

