2
votes

I need to hook into the Creating event in Orchard CMS and use the Title of the content that's being created (Along with some other properties) to create an item in a 3rd party system and return the ID of that to set in the content type in Orchard.

I have a custom content type, but when I try to hook into the events (as explained in the docs here and also by looking at code on the built in Orchard Core content parts) all properties are null.

Are they just lazy loaded? Is there a way to populate them? Overriding any of the shape methods (GetItemMetadata / BuildDisplayShape / BuildEditorShape / UpdateEditorShape) doesn't seem right as this should only fire when initially creating the content type.

My code is:

public MyContentPartHandler(IRepository<MyContentPartRecord> repository, IOrchardServices orchardServices, Lazy<IMyContentPartService> myContentPartService) {
    _orchardServices = orchardServices;
    _myContentPartService = myContentPartService;
    Filters.Add(StorageFilter.For(repository));

    OnCreating<MyContentPart>(CreateTPItemAndAssignIdentity);
}
protected void CreateTPItemAndAssignIdentity(CreateContentContext context, MyContentPart part)
{
    //create item in 3rd party system
    var item = _myContentPartService.Value.CreateNewItem(part.Title, part.Path);
    part.ExternalIdentity = item.FriendlyId;
}

The CreateNewItem() method fails as the part.Title and part.Path are null. Do I need to try and get at the Record? (I thought not, as the Orchard CMS record has not been created at that point)

UPDATE - I have also tried to use the OnCreated event instead, but I run into the same problem with the properties not being populated. I place a breakpoint in the code, and noticed that when the OnCreated breakpoint was hit - the data did not actually exist in the database at that time.

1
I think that it is better to use OnCreated, beacuse at that point you should have all the other parts loaded.mberacochea
just tried it in OnCreated and no change - none of the properties are populated. :/ I will update the original question too to show the attempt - thanks for the suggestion thoughzanther

1 Answers

2
votes

OnCreating/OnCreated events are fired when an item object is constructed, not populated with data.

If you want to be sure that data is already there, use either OnPublished event (fired after item has been updated) or OnLoading/OnLoaded events if you want to run some code after an item has been fully loaded from database.

This description of certain events might also be helpful.