Bypass Custom Global Events

Brenden Kehren asked on May 21, 2015 14:14

I have a global event handler that captures when a cms.user is updated. I have a specific time within 2 other methods when I don't want this global event handler code to run. How can I bypass that? I've tried this and no luck, it just doesn't log it to the event log:

private void DoUserUpdate(string value, int userId)
{
    UserInfo ui = UserInfoProvider.GetUserInfo(userId);
    ui.Email = value;
    using (new CMSActionContext { LogEvents = false })
    {
        UserInfoProvider.SetUserInfo(ui);
    }
}

Recent Answers


Charles Matvchuk answered on May 21, 2015 17:23

You could just put in an override ? in your call, or throw some logic in the Global Event Handler before it makes the call to the method.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 22, 2015 15:54

The catch is Charles, I want to only do this for 1 or 2 specific instances but any other event, I want the normal global update event override to take place. My global event handler catches anytime a cms.user or cms.usersettings object is updated and performs additional work after the update happens. The method above is one of the two instances in which I don't want those global events to happen. There is nothing in the cms.user orcms.usersettings objects that in which I can catch to determine where the call came from.

So I see my 2 options are to create a standard SQL UPDATE statement and use the ConnectionHelper.ExecuteNonQuery() or add a flag to the cms.user object to say it's been updated by one of those 2 methods.

1 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on May 23, 2015 05:29 (last edited on May 23, 2015 05:29)

How about a trigger on the database table that you toggle, just add a parameter that it can pick up to disable trigger? You could even have the trigger update a flag.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 23, 2015 17:08

Thanks for the suggestion Charles. Personally I don't use database triggers because they are not easily seen and are a last ditch effort when I can't find a way to add my business logic in code.

1 votesVote for this answer Mark as a Correct answer

Richard Sustek answered on June 2, 2015 10:22 (last edited on June 2, 2015 10:23)

Hi Brenden,

This is not possible out of the box, but I have been able to use quite a simple and elegant workaround by passing in the RequestStockHelper values between pages. The idea is that in your method you use something like:

        var user = UserInfoProvider.GetUserInfo("Andy");
        if (user != null)
        {
            RequestStockHelper.Add("DontFireGlobalEvent", "1");
            user.FirstName = "new first name"
            user.Update();
        }

And in global event you get the value out of RequestStockHelper using:

var useUpdateEvent = ValidationHelper.GetString(RequestStockHelper.GetItem("UseUpdateEvent"), "0");

Now all you need to do is to create an "if" condition inside the global event in order to see if your code should get executed. The global event itself will of course get fired so its not exactly bypassing it, but it should do the work.

Additionally we have a CMSActionContext which can be used to stop certain logging (WF tasks, Synchronization tasks, Event log events etc..) which can be used like:

    using (var ac = new CMSActionContext(){
        LogEvents = false,
        LogSynchronization = false,
        LogIntegration = false,
        LogWebFarmTasks = false
    })
    {
       // code here
    }      

However there is no trigger for global events. This might be interesting though if you would be in a similar situation with other events.

Hope it helps!

Kind regards,

Richard Sustek

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.