Custom Event Listener
Learn about creating your own Event Listener to suit your needs.
Add a custom listener
Writing code for custom event listeners
Script binding
The script binding is a set of variables you can use in your custom script. An event variable in the script binding corresponds to the event which triggered the listener. For example, if you are listening for PageCreateEvent the event variable will be a
PageCreateEvent
object.
Multiple Events
You can choose to have your handler listen for multiple events. If you need to do different things depending on the type of event, you can check that with instanceof. Alternatively, you can type the event variable to the most specific superclass. In the previous example, that would be
PageEvent
.
In the following example, the event is getting page content for both PageCreateEvent and PageUpdateEvent. Since PageEvent is a common superclass for both PageCreateEvent and PageUpdateEvent, you could cover both events by using the following code:
// The event is passed in the binding. This line is only used to give type information when using an IDE, and has no functional impact.
def event = event as PageEvent
def content = event.content.bodyAsString
// do something with the page contentExamples
Add a comment when a banned word is used
Some organizations have a particular style guide or would like to enforce specific rules. This event listener example looks at the content of new pages for banned words. If the page content contains any on a list of banned words, a comment is automatically added with an alternative suggestion.
Follow these steps to create the listener:
The following image contains a comment generated in response to a banned word:
Add an inline comment when a banned word is used
Similar to the previous example, you can configure your listener to add inline comments instead. As you get these comments, you can dismiss them.
Follow these steps to create the listener:
The following image is an example of an inline comment:
Create a page in a space when a user is created
This event listener example automatically creates a user profile page in the Team space. You can use this profile page to list their skills and profile.
Follow these steps to create the listener:
The following image is an example of a profile page created when the jbloggs user is created:
Collect stats
This event listener example automatically sends statistics to statsd for page views, space views, and users/pages views.
Follow these steps to create the listener:
The following image is an example of the statistics for page views: