1

I am using Sitecore 9.1.1

The issue is, I have a custom eventhandler for onPublishEnd where I set the value of a certain field (Ex: Field A) by using BeginEdit() and EndEdit(), which in turn triggers the onSaving event.

I also have a custom onSaving event where I change the value of another field in the item (Ex: Field B).

What happens is, because EndEdit() in my custom onPublish triggers the onSaving event, my item is changed twice. I need a way for my item to only be changed when onSaving is not triggered from onPublish.

Is there a way to capture the name of the event that triggered another event? Can I check inside the onSaving custom event if it was triggered by onPublish or not?

  • What you're doing, you really shouldn't be doing. You should hook into the uiSaved pipeline rather than relying on events like this. That said; your Context.Site.Name will be publisher when publish is running. – Mark Cassidy May 29 '19 at 11:17
  • @MarkCassidy Thanks for the reply, I tried using the UiSaved pipeline, but for some reason it doesnt save the changes to the item at all, even though i use BeginEdit and EndEdit, and I tried adding SecurityDisabler as well, the changes dont reflect at all..any idea why? – Mie ElMansy May 29 '19 at 12:33
  • Well I know you don't need to use BeginEdit/EndEdit, the item is already in that state. Be mindful you're grabbing the correct item - see this article for an example: blog.krusen.dk/hooking-sitecores-save-pipeline – Mark Cassidy May 29 '19 at 12:42
  • @MarkCassidy when I didn't use BeginEdit/EndEdit I would always get an exception that the item is not in editing mode, however i got it eventually working by modifiying the processor configuration, I'm writing an answer now.. thanks for your help! – Mie ElMansy May 29 '19 at 13:25
1

The answer to my original question could have worked as Mark suggested, by looking at Context.Site.Name during the onSaving event, but then I tried to go with the best practice and use UISaved pipeline, as it triggers only when the Sitecore user "Saves" from within the Sitecore Content editor, so that worked perfectly for my case.

I was trying to change my workflow state field, but for some reason the changes wouldn't reflect in the item after saving, however this is how it finally worked:

I added the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Caching;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Pipelines.Save;

namespace Example.Workflows
{
    public class SaveUIProcessor
    {   
        public virtual void Process(SaveArgs args)
        {
            foreach (SaveArgs.SaveItem saveItem in args.Items)
            {
                var item = Sitecore.Context.ContentDatabase.Items[saveItem.ID, saveItem.Language, saveItem.Version];
                if (item != null)
                {
                    setWorkflowState(item);
                }
            }
        }

        protected virtual void setWorkflowState(Item item)
        {
                using (new Sitecore.SecurityModel.SecurityDisabler())
                {
                    try
                    {
                        item.Editing.BeginEdit();
                        item.Fields["__Workflow"].Value = WorkflowID;
                        item.Fields["__Workflow State"].Value = ReviewStateID;
                        item.Editing.EndEdit(true,false);
                    }
                    catch(Exception ex)
                    {
                        Sitecore.Diagnostics.Log.SingleError("Error in workflow state set on saveui" + ex, ex);
                    }
                }
         }
    }
} 

I also added the following patch in a .config file for the processor, it ensures that my custom code runs before Sitecore.Pipelines.Save.WorkflowSaveCommand:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <processors>
      <saveUI>
        <processor mode="on" type="Example.Workflows.SaveUIProcessor, Example.Workflows" patch:before="processor[@type='Sitecore.Pipelines.Save.WorkflowSaveCommand, Sitecore.Kernel']" />
      </saveUI>
    </processors>
  </sitecore>
</configuration> 

After doing this, the changes reflected successfully.

this answer was also pretty helpful https://sitecore.stackexchange.com/a/923/3547

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.