4

We are trying to add in additional code everytime an item moves into Editing state of the workflow.

When does this happen? Would I add this code on item save? Version added? Admin users usually dont trigger workflow state updates - this leads me to believe that its not a plain and simple item save hook?

In my scenario, the item being edited actually doesnt have a workflow on it, but whenever it is edited which would virtually move it into editing state if it had a workflow, I need to add some custom code to add its parent item (which has workflow set on it) into editing state.

3

You should read this article from John West: https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/repost-intercepting-item-updates-with-sitecore

It explains what your options are to intercept item saves. There is no real best answer for everything, it depends what you want to do. It mentions the events: item:saved and item:saving. (more info in Hishaam's answer) But also the rules engine and the saveUI pipeline.

As far as I know, there is no pipeline for workflow - in the <saveUI> pipeline you will find the WorkflowSaveCommand - this answers your question but won't get you what you need.

When I read your question, I assume that you have a rather specific item template that you want this code for. In that case, I would go for the item saved rule. You can achieve the same result with the events, but you might need to write some additional checks which you could turn into a condition to separate the condition from the actual action.

|improve this answer|||||
4

You should use the event item:saved. When you have saved the item, in the handler, you will be able to extract the item which has been saved. Based on the item, you can retrieve the parent item. Then, check if the parent item's template is derived from a template that you want to update the workflow.

The reason you need to check for the parent item is becaue you don't want the workflow state to be updated for all the items.

public void OnItemSaved(object sender, EventArgs args)
{
    var eventArgs = args as SitecoreEventArgs;

    var item = eventArgs?.Parameters[0] as Item;

    if (item != null)
    {
        var parentItem = item.Parent;

        if (ProcessShouldSkipped(parentItem))
        {
            return;
        }

        using (new SecurityDisabler())
        {
            parentItem.Editing.BeginEdit();
            parentItem.Fields["__Workflow state"].Value = "ID of your workflow state";
            parentItem.Editing.EndEdit(true);
        }
    }
}

public static bool ProcessShouldSkipped(Item parentItem)
{
    return !parentItem.Template.ID.Equals(new ID("ID of your template"));
}

And you need to patch the new handler to the item:saved event.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <events>
        <event name="item:saved">
            <handler type="YourNamespaceWithClassName,YourAssemblyName" method="OnItemSaved" />
        </event>
    </events>
  </sitecore>
</configuration>

Note that in this code snippet the template ID has been hard coded. You can create a setting either in Siteceore or in a config file. Then use the setting to see if the item workflow should be updated.

|improve this answer|||||
  • I presume this event is also called when an item is approved / goes through workflow state change alone also? – Durga Bose Sep 23 '18 at 14:13
  • 1
    This is why when calling the EndEdit method, the parameter is set to true. This will not trigger trigger the event. So it will not end up in a loop – Hishaam Namooya Sep 23 '18 at 14:38

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.