1

I'm using Sitecore 9.1.1, and in the sitecore.config there are a series of user events (user:created, user:created:remote, user:deleted, user:deleted:remote, user:updated, user:updated:remote, etc). Each entry has a comment that suggests the type of input it takes...for example, for user:created, it shows a comment of user:created(System.Web.Security.MembershipUser user). I created a method like the following:

namespace Foundation.Core.Events {
  public class UserEvents {
    private readonly ILogRepository _logRepository;

    public UserEvents(ILogRepository logRepository) {
        _logRepository = logRepository;
    }

    public void OnUserCreated(System.Web.Security.MembershipUser user) {
        _logRepository.Info("OnUserCreated fired");
    }
  }
}

And did a patch as follows:

  <sitecore>
    <events>
        <event name="user:created">
            <handler type="Foundation.Core.Events.UserEvents, Foundation.Core" method="OnUserCreated" resolve="true" />
        </event>
    </events>
  </sitecore>

When I built out the code, I got back the following error:

Could not instantiate event handler. Type: Foundation.Core.Events.UserEvents. Method: OnUserCreated (method: Sitecore.Events.Event.BuildConfigSubscribersFromConfiguration()).

I tried paring the method down to remove the dependency injection and constructor, in case that was causing an issue, but I get the same error regardless. Since there are no "live" events in here, there's no direct pattern to follow, but I went with the definition from Sitecore's config as a guidance.

Has anyone ever used these events, and if so how did you tie into the system?

1 Answer 1

2

Any event handler has to follow a few conventions:

The class must have a method that accepts two parameters and return void:

  1. object - represents the object that holds a collection of the various event listeners
  2. EventArgs - holds the parameters being passed to the event handler

For your case, if you added the following event handlers for the user:created or user:created:remote events in a Sitecore patch file

 <sitecore>
    <events>
        <event name="user:created">
            <handler type="Foundation.Core.Events.UserEvents, Foundation.Core" method="OnUserCreated" resolve="true" />
        </event>
        <event name="user:created:remote">
            <handler type="Foundation.Core.Events.UserEvents, Foundation.Core" method="OnUserCreatedRemote" resolve=“true” />
        </event>
    </events>
  </sitecore>

their implementation is supposed to be like

namespace Foundation.Core.Events 
{
  public class UserEvents 
  {
    ...
    public void OnUserCreated(Object o, EventArgs args) 
    {
       var scEventArgs = (SitecoreEventArgs)args;
       var user = (MembershipUser)scEventArgs.Parameters[0];
        _logRepository.Info("OnUserCreated: " + user.UserName);
    }

    public void OnUserCreatedRemote(Object o, EventArgs args) 
    {
       var scEventArgs = (SitecoreEventArgs)args;
       var remoteEventArgs = (UserCreatedRemoteEventArgs)scEventArgs.Parameters[0];
       _logRepository.Info("OnUserCreatedRemote: " + remoteEventArgs.UserName);
    }
  }
}
1
  • Thanks, really good answer. Are you sure you aren't the Sitecore support engineer who told that to me? :) I heard from them after I hadn't gotten an answer here, but this is very correct and I do have it working now. Cheers! Commented Dec 22, 2019 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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