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.