0
我正在研究一種機制,根據對該項目的字段檢查進行更改和項目圖標。如果我檢查的Referencefield有一個targetItem,而targetItem有內容,我希望我要保存的項目的圖標被更改。這是我當前的代碼:Sitecore事件 - 保存的項目引發TargetInvocation異常 - > IconChange
Sitecore.Data.Items.Item currentItem = Sitecore.Events.Event.ExtractParameter(args, 0) as Sitecore.Data.Items.Item;
if (ValidForIconChange(currentItem))
{
using (new EventDisabler())
{
if (HelperClasses.SharedContentHelper.itemHasSharedContent(currentItem))
{
currentItem.Editing.BeginEdit();
currentItem.Appearance.Icon = "Applications/32x32/document_out.png";
currentItem.Editing.EndEdit();
}
else
{
currentItem.Editing.BeginEdit();
currentItem.Appearance.Icon = currentItem.Template.Icon;
currentItem.Editing.EndEdit();
}
}
RefreshScreen(currentItem);
}
和輔助功能:
/// <summary>
/// Checks if the item is valid to change the icon of the item
/// </summary>
/// <param name="currentItem">The current item.</param>
/// <returns></returns>
private static bool ValidForIconChange(Sitecore.Data.Items.Item currentItem)
{
return currentItem != null && ItemHelper.DoesItemImplementTemplate(currentItem, "_gedeelde inhoud") && currentItem.Language.Name == "nl-NL";
}
/// <summary>
/// Refreshes the screen.
/// </summary>
/// <param name="currentItem">The current item.</param>
private void RefreshScreen(Sitecore.Data.Items.Item currentItem)
{
if (Sitecore.Context.ClientPage != null)
{
if (currentItem.Parent != null)
{
if (!currentItem.Editing.IsEditing)
{
//TODO: set to the appropriate item
String refresh = String.Format("item:refreshchildren(id={0})", currentItem.Parent.ID);
Sitecore.Context.ClientPage.SendMessage(this, refresh);
}
}
}
}
目前我只是猜測了一下週圍就如何與此事件的工作。基本上,我試圖找出如何使用EventDisabler,因爲我想提出自己的事件來更新父母孩子,因爲我更改了圖標後,但是這會給我帶來麻煩,因爲我在發佈網站時。我想我必須知道何時使用發佈操作而不是單個項目保存。我只想在真正保存一個項目時而不是在發佈網站時觸發此代碼。發佈行爲導致以下錯誤(我以爲是因爲我停用事件,併發布事件後引發):這裏
Job started: Publish to 'web'|Items created: 0|Items deleted: 0|Items updated: 1|Items skipped: 0|Job ended: Publish to 'web' (units processed: 1)|Job started: Publish to 'web'|#Exception: System.Reflection.TargetInvocationException: Het doel van een aanroep heeft een uitzondering veroorzaakt. ---> System.NullReferenceException: De objectverwijzing is niet op een exemplaar van een object ingesteld.
bij Sitecore.Web.UI.Sheer.ClientPage..ctor()
bij Sitecore.Context.get_ClientPage()
bij CommandTemplates.Classes.SharedContentIconChanger.RefreshScreen(Item currentItem)
bij CommandTemplates.Classes.SharedContentIconChanger.OnItemSaved(Object sender, EventArgs args)
bij Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters, EventResult result)
bij Sitecore.Events.Event.EventSubscribers.RaiseEvent(String eventName, Object[] parameters)
bij Sitecore.Events.Event.RaiseEvent(String eventName, Object[] parameters)
bij Sitecore.Events.Event.RaiseItemSaved(Object sender, ItemSavedEventArgs args)
bij Sitecore.Events.Event.DataEngine_ItemSaved(Object sender, ExecutedEventArgs`1 e)
bij System.EventHandler`1.Invoke(Object sender, TEventArgs e)
bij Sitecore.Data.Engines.EngineCommand`2.RaiseExecuted()
bij Sitecore.Data.Engines.EngineCommand`2.Executed()
bij Sitecore.Data.Engines.EngineCommand`2.Execute()
bij Sitecore.Data.Engines.DataEngine.SaveItem(Item item)
bij Sitecore.Data.Managers.ItemProvider.SaveItem(Item item)
bij Sitecore.Data.Managers.ItemManager.SaveItem(Item item)
bij Sitecore.Data.Items.ItemEditing.AcceptChanges(Boolean updateStatistics, Boolean silent)
bij Sitecore.Data.Items.ItemEditing.EndEdit(Boolean updateStatistics, Boolean silent)
bij Sitecore.Data.Items.EditContext.Dispose()
bij Sitecore.Publishing.PublishHelper.CopyToTarget(Item sourceVersion)
bij Sitecore.Publishing.PublishHelper.PublishVersionToTarget(Item sourceVersion, Item targetItem, Boolean targetCreated)
bij Sitecore.Publishing.PublishHelper.PublishVersion(Item sourceVersion)
bij Sitecore.Publishing.Pipelines.PublishItem.PerformAction.PublishVersion(PublishItemContext context)
bij Sitecore.Publishing.Pipelines.PublishItem.PerformAction.ExecuteAction(PublishItemContext context)
bij Sitecore.Publishing.Pipelines.PublishItem.PerformAction.Process(PublishItemContext context)
bij (Object , Object[])
bij Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters)
bij Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
bij Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
bij Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args)
bij Sitecore.Publishing.Pipelines.PublishItem.PublishItemPipeline.Run(PublishItemContext context)
bij Sitecore.Publishing.Pipelines.Publish.ProcessQueue.ProcessEntries(IEnumerable`1 entries, PublishContext context)
bij Sitecore.Publishing.Pipelines.Publish.ProcessQueue.Process(PublishContext context)
bij (Object , Object[])
bij Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters)
bij Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
bij Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
bij Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args)
bij Sitecore.Publishing.Pipelines.Publish.PublishPipeline.Run(PublishContext context)
bij Sitecore.Publishing.Publisher.PerformPublish()
bij Sitecore.Publishing.Publisher.Publish()
--- Einde van intern uitzonderingsstackpad ---
bij System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
bij System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bij Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj)
bij Sitecore.Reflection.MethodInstance.Invoke()
bij Sitecore.Jobs.JobRunner.RunMethod(JobArgs args)
bij (Object , Object[])
bij Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters)
bij Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
bij Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args, String pipelineDomain)
bij Sitecore.Jobs.Job.ThreadEntry(Object state)
任何人有不同的方法或任何提示的想法? 謝謝!
你無法進入ClientPage ctor嗎?在RefreshScreen的這一行嘗試創建一個之後,會有一個nullref異常:'Sitecore.Context.ClientPage.SendMessage(this,refresh);' – mtijn
這是因爲沒有客戶端:)。我解決了這個問題,無論如何。 – Younes