Umbraco: more event goodness!

More event-based programming. Can’t get enough of it!

On a more serious note, as an adjunct to my previous post about page events, I want to go into details (as much I can, really) on how I also made use of the Document events in Umbraco.

It seems like editors/webmasters are more demanding these days when it comes to doing their task automatically in their CMS. My latest project was no exception. Basically, the requirement was to do four different tasks in two clicks three clicks (right click -> Create -> Create)

What we want to achieve in these three clicks is:

  • Create a root document
  • Create a corresponding UserGroup
  • Create a news section under the root document
  • Set permissions to the created UserGroup on the root document

Of course, we could also use the Relationship API to relate members to the root document, but since we’re going to use the built-in ASP.NET MemberShip provider, a corresponding usergroup would be the best option (at least in my opinion)

So, on to the event stuff! Doing this is actually quite simple using the before-mentioned event model in Umbraco.

First, create a new class (I called mine ‘DocumentEvents’), we need this class to inherit from the ApplicationBase found in the umbraco.BusinessLogic namespace. In the constructor of the class, we’ll hook up to the Document.AfterPublish and Document.AfterDelete events.

Let’s first have a look at the Document.AfterPublish event. First, we make sure that the document in question is of type “YourDocumentAlias” so we won’t be creating a new usergroup for every (other) document being published ;-) Next, we create a new usergroup with the same name as our Document as long as it doesn’t already exist. Then we create the news root node as a childnode of the document being published and at last, we set the permissions so only the members of our new usergroup may access our new rootnode and all it’s childnodes.

Onto the Document.AfterDelete event. Whenever an editor/administrator deletes the rootnode, we want to make sure he deletes the usergroup aswell. All we have to do in the event is to check if the usergroup exists and if it does, delete it.

And that’s basically all there is to it. This post is just an example of what we’re able to do with the event model in Umbraco. Basically, by using the event model, you should be able to publish whole sections in one go and even a whole site, maybe? :-)

This entry was posted in ASP.NET, Umbraco CMS and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


4 + = ten