0

I have a list with an ItemUpdating event receiver that blocks all item updates.

I have a workflow where I want to update an item in this list.

Is there any way to disable the event receiver from the SP2013 workflow, perform the update, then re-enable the event receiver?

Or, if there was a way to check whether it is a user or a workflow that is making the update, I could add a condition in the event receiver and only allow updates from workflows.

  • You mean don't execute event receiver in case of updates made by the workflow? – Amal Hashim Dec 31 '14 at 13:30
  • No, the ItemUpdating event will still fire, but in there I'm currently setting properties.Cancel to true. If I knew the event was triggered by a workflow I wouldn't cancel the updating event. – Submits Dec 31 '14 at 18:36
  • Sorry, my response was referring to my second possible solution. With regards to the first, yes if the event receiver could not be fired if the update is made by a workflow that would be perfect. – Submits Dec 31 '14 at 18:42
2

The easiest workaround is to add a hidden field into the list. And let the workflow update a predefined value into that field.

In the event receiver check the hidden field is equal to predefined value. Then that means it's been invoked from the workflow. Then reset the field to empty so the next update can be validated using the same field.

  • That won't work because the updating of the field will be cancelled by the event receiver. This is exactly the problem I'm having. – Submits Dec 31 '14 at 18:49
  • Before you cancel why can't you do the checking? Can you post your event receiver code. – Amal Hashim Dec 31 '14 at 18:51
  • It's not the checking of the hidden field that would be the problem. It's the setting of the hidden field from the workflow that would. As soon as you try to do this the event will fire and the workflow will go into suspended status. I will post code soon, but it's literally just 3 lines of code to set an error message and then cancel the event. – Submits Dec 31 '14 at 19:02
  • You can change the sequence.. I.e whether the workflow should run or the receiver should run first. Anyway post the code and will try to modify to make it work. – Amal Hashim Dec 31 '14 at 19:04
  • Your suggestion allowed me to put the solution together - thank you! 1. Create a hidden field in the list 2. In the workflow, when you go to edit the list item, also put something into the hidden field 3. In the ItemUpdating event receiver check this hidden field in properties.AfterProperties – Submits Jan 2 '15 at 10:05
1

Create a class that derives from SPItemEventReceiver and IDisposable. Example:

public class EventReceiverScope : SPItemEventReceiver, IDisposable
{
    private bool mOriginal = false;

    public EventReceiverScope(bool enabled)
    {
        this.mOriginal = this.EventFiringEnabled;
        this.EventFiringEnabled = enabled;
    }

    public void Dispose()
    {
        this.EventFiringEnabled = this.mOriginal;
    }
}

Then call the above class like this in the Workflow code:

using (EventReceiverScope scope = new EventReceiverScope(false))
{
    //WF code here to perform update
}
  • How do I do this with a SharePoint 2013 workflow since custom custom code is not allowed? – Submits Dec 31 '14 at 18:37
  • Obviously this can be used in code based workflow – Nadeem Yousuf Jan 1 '15 at 2:48
0

In the end, the event receiver looked like this:

    public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);

        var fromWorkflow = properties.AfterProperties["hFromWorkflow"];

        if (fromWorkflow != null)
        {
            if (fromWorkflow.ToString() != "y")
            {                  
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "A message";
            }
            else
                properties.AfterProperties["hFromWorkflow"] = "n";
        }
    }

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.