Sitecore and Events? Creating your own events and how to use them.

Using events can be useful when you need to run multiple operations on a item, if something changes or happens in your solution.

In Sitecore you can use the Sitecore.Events.Event namespace to create your own events, execute them when you need to and add event listeners as needed.

Registering the event:

    <event name="customevent:post">
       <handler type="Business.Pipelines.Events.PostEvent, Business" method="OnPost" />

All you need to add an event, is to add your own config element with the name of your event, pointing at the implementation.

Implementing the event:

public virtual void OnPost(object sender, EventArgs args)
   var parameters = Event.ExtractParameters(args);            
   _users = parameters.Length > 0 ? parameters[0] as IEnumerable<User> : null; 
   _itemContext = parameters.Length > 1 ? parameters[1] as Item : null;

When implementing the event, you can access any parameters provided when raising the event. In this case we need a list for users along with a item.

Raising the event:

Sitecore.Events.Event.RaiseEvent("customevent:post", TargetUsers, item)

After the event have been raised, Sitecore handles everything needed to call your listeners.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s