开发者

Sitecore Event - Item Saved raises TargetInvocation Exception -> IconChange

I'm working on a mechanism to change and items Icon based on a check on a field of that item. If the Referencefield I'm checking has a targetItem and the targetItem has content I want the icon of the item I'm saving to be changed. This is my current code:

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

And the helper functions:

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

Currently I'm just guessing around a bit on how to work with this event. Basicly I´m trying to find out on how to work with the EventDisabler, cause I want to raise my own event for updating the parents children after I´ve changed the icon, but this gives me trouble for when I´m publishing the website. I think I haveto know when I´m working with a publish action instead of a single item save. I want this code only to be triggered when really just saving an item and not when publishing the website. Publish actions result in the following error (which I assume are because I disable events, and after publishing an event is raised):

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)

Anyone here have an idea on a different approach or any tips? Thanks!


Solved this issue by checking wheter item:saved was called by publishing or just by saving the item:

if(Sitecore.Context.Job != null &&Sitecore.Context.Job.Category == "publish") 
{
  // Do nothing
} else {
  // Change Icon code here`enter code here`
}

This way you can determine if you're in the proces of publishing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜