Access to the newly created part in the Handler via OnCreated event

Topics: Customizing Orchard, Writing modules
Jan 10, 2013 at 8:22 AM

Hi!

I am trying to perform som related task when a custom part is created, by providing a method to the OnCreated event of the PartHandler class for the custom part in question. The problem is, the part and the CreatContentContext passed to my method are null. 
Is there any other event in the PartHandler class close to the creation state of a part that i can use which give me access to the newly created part?
I tried the same process with the OnCreating event and i get the same result. 

   OnCreated<CustomPart>(RunRelatedTask)   //inside PartHandler constructor

   private void RunRelatedTask(CreateContentContext context, CustomPart part)
     { 
            // I need access to the newly created part here but it is null.
     }

 I Hope my question is clear and thanks in advance.

Developer
Jan 10, 2013 at 10:06 AM

That is weird, context and part should not be null when that method is invoked. If you look at usages of OnCreated<TPart>, you will find many occasions. What you could try is compare your implementation with theirs, set some breakpoints and trace back your steps, both from your code as well as from existing code, and compare differences.

Jan 10, 2013 at 11:09 AM

Thanks for the quick response. After trying your suggestion, i can see now that the part itself is not null but its fields are. The contentItem it is attached to and the part itself are properly created after the request is done, but at that specific moment when my method is called, the value of the fields are not there yet.
 The thing is, doing the same for the OnRemoved event gives me the part with all the fields set to the proper value. Is there any other place in the event chain, maybe right after the creation beside the OnCreated event that i can hook into to get access to the part?

Thanks.

Jan 11, 2013 at 9:37 AM

After further tracing, i found that the OnCreated event is fired when a contentItem is created instead of what i had in mind. To clarify, the custom partRecord itself is not persisted yet at this stage but instead a contentItemRecord is created and used later on to represent the part or something like that. That's my interpretation of the code but i may be wrong, just a newbie here.
I moved my code to my Service class that takes care of creating the custom contentType. This solution is not good but it's working for the moment.

Thanks for the help.

Developer
Jan 11, 2013 at 11:13 PM

Perhaps try the [OnLoaded] event?