Using oncreated event in a part

Topics: Writing modules
Aug 17, 2011 at 9:37 PM

When a new item of type "Article" is created in our site, we want to run a function/service call that looks up some stuff based on the post slug of the content item you're adding and sets a property on one of the parts (we'll call it "TestPart") that exists on this content item.

So it goes as follows:

New Article (Imported, or created via the admin) -> call service _myService.CheckPostSlug(postSlug) -> based on the CheckPostSlug result, set a propery on the newly created item's "TestPart" and persist that to the datastore.

I was looking into OnCreated in the handler, thinking that all I really have to do is:

protected override void Created(CreateContentContext context) {
            context.ContentItem.As<TestPart>().TestProperty = _myService.CheckPostSlug(context.ContentItem);

Now that the property is set in there, how do I persist that TestPart to the datastore? I thought it might automatically do that for me, but it doesn't seem to.

This is kind of tough to explain, I hope I was able to state it clearly. 

Aug 17, 2011 at 11:57 PM

Does this TestPart have a ContentPartRecord, and a Handler registering a Repository for it ?

Aug 19, 2011 at 1:59 PM

@tjans As Sebastien mentioned you need to register the repository for your ContentPartRecord. So assuming that TestPart inherits from ContentPart<TestPartRecord>, and that TestPartRecord inherits from ContentPartRecord ... you can simply create a constructor in your handler that looks like this:

public TestPartHandler(IRepository<TestPartRecord> repository) {
Once you do that, then it should automatically persist the TestPart for you.

Aug 22, 2011 at 2:41 PM

This topic is not abandoned, I was out on vacation a few days...

Yes, it does have a handler registering a repository, but for some reason it's not saving it...I don't have to explicitly call save, right? If I handle the oncreated and just set the property, it should persist it for me after the item is created?  I'll take another glance at my code and if I have more struggles with it, I'll post the code...

Aug 22, 2011 at 2:50 PM

I'll just post the code anyways:


 public class ContentCategoryHandler : ContentHandler
        private readonly ICategoryService _categoryService;

        public ContentCategoryHandler(IRepository<ContentCategoryPartRecord> repository,
                                      ICategoryService categoryService) {

            _categoryService = categoryService;

        protected override void Created(CreateContentContext context) {
            context.ContentItem.As<ContentCategoryPart>().Category = _categoryService.GetParentCategory(context.ContentItem);

l would expect that code to set the category property on my ContentCategoryPart and persist that to the database (on the save of the content item?)


Aug 22, 2011 at 3:12 PM

That seems correct. And ContentCategoryPartRecord inherits from ContentPartRecord, correct? If that's the case, it should be persisted. Have you tried debugging and ensuring the Created method is getting called?

Aug 22, 2011 at 3:29 PM

Yeah, I did that, it hits it...though I can't remember the order of things that happened (4 day absence fried my brain).  I'll start fresh today and gather some more info...

Aug 22, 2011 at 3:54 PM
Edited Aug 22, 2011 at 4:03 PM

Something very weird is happening, and now that I see it, I remember it happening before I left on vacation as well.  I set a breakpoint in my "OnCreated" and it seems to fire it while I'm in the process of filling out the form for adding my new content item...specifically, it triggers when I lose focus of my Title part...

It performs an ajax post to admin/common/routable/slugify...checking to see if that slug exists, maybe? Why would it hit my "oncreated" event then...someone else on this project also claimed it hit his published event as well.  It doesn't seem to persist anything when this happens...anyways, back to the lecture at hand...

Aug 22, 2011 at 4:33 PM

Back to the original problem, I have found where it's not persisting correctly. When it gets into the Editor post method, the part.Category property is set correctly as it should be from the OnCreated event.  After I run updater.TryUpdateModel(part, Prefix, null, null), it gets rest (probably because tryupdatemodel rebuilds the part from the form).

Aug 22, 2011 at 7:23 PM

The something very weird is the Ajax call that generates the slug from the title. It does create and publish the item, then rolls back the transaction, so that we are in the conditions where the item is created, in order to get the slug right.

Aug 22, 2011 at 7:27 PM

Ahh, so that's why it doesn't get published, makes sense.  Thanks...

Aug 22, 2011 at 7:44 PM
Edited Aug 22, 2011 at 7:47 PM

On a side note, I'm able to step through my code on an import of my content item to see that it hits my oncreated event, but it's failing because I'm using the RoutePart of the content item to get at the path to do some processing, and at that time, the "path" is null.   Is there another way to get at the path of the item at that point in time? This doesn't error out when adding the content item manually, only when it is imported...

It makes sense because the item isn't saved yet, but I need some other way to get to it...

Aug 22, 2011 at 7:49 PM

Subscribe to a later event?

Aug 22, 2011 at 8:37 PM

Yeah, I've settled on using "published"