ContentService Created Event - Entity doesn't have ID
I was assuming that the CREATED event would fire and that document would have an ID alread. Is there a way to create child documents without having to check if they exist first. I was hoping the CREATED event would work for this.
The example below won't work because I'm guessing the Document hasn't been saved yet?
public class EventStartup : IApplicationStartupHandler
{
public EventStartup()
{
ContentService.Created +=ContentService_Created;
}
private void ContentService_Created(IContentService sender, Core.Events.NewEventArgs<Core.Models.IContent> e)
{
if (e.Entity.ContentType.Alias == "Project")
{
var Messages = sender.CreateContent("Messages", e.Entity.Id, "Messages");
sender.SaveAndPublish(Messages);
}
}
}
}
With regards to the CreateContent (and CreateMedia) methods - its actually by design that the returned Content (or Media) object doesn't have an Id. The methods are intended as a convenient way of creating a content object based on a given ContentType without triggering a save operation against the database as you would normally want to update the content object in some way prior to it being saved.
If you are looking for an event to use when a user has created a new document in the backoffice you should use the Saved event.
For the upcoming version 6.0.6 release we have added a few new methods that you can use to create a content object with an Id called CreateContentWithIdentity.
With regards to the specific example:
var Messages = sender.CreateContent("Messages", e.Entity.Id,"Messages"); sender.SaveAndPublish(Messages);
we have fixed an issue in the SaveAndPublish method, which allows you to pass in a Content object without an identity. So I believe everything in question should be fixed in version 6.0.6.
Sorry to bring up the subject again but is there anyway to tell whether an Entity within the SaveAndPublish event has just been created? For example, I'd like some code (using the new Page Id) to execute only when a page has first been created. The Entity passed within the Created event seems to have a Page Id of 0.
ContentService Created Event - Entity doesn't have ID
I was assuming that the CREATED event would fire and that document would have an ID alread. Is there a way to create child documents without having to check if they exist first. I was hoping the CREATED event would work for this.
The example below won't work because I'm guessing the Document hasn't been saved yet?
This is a known bug and will be fixed with v6.0.1.
Thanks for the response..
I'm using v6.0.2 and it has not been fixed yet. Trying to find the issue on the issues.umbraco...
I'm using 6.0.5 and the issue still persists. Just an FYI.
When will the bug be fixed?
Hi all,
With regards to the CreateContent (and CreateMedia) methods - its actually by design that the returned Content (or Media) object doesn't have an Id. The methods are intended as a convenient way of creating a content object based on a given ContentType without triggering a save operation against the database as you would normally want to update the content object in some way prior to it being saved.
If you are looking for an event to use when a user has created a new document in the backoffice you should use the Saved event.
For the upcoming version 6.0.6 release we have added a few new methods that you can use to create a content object with an Id called CreateContentWithIdentity.
With regards to the specific example:
we have fixed an issue in the SaveAndPublish method, which allows you to pass in a Content object without an identity. So I believe everything in question should be fixed in version 6.0.6.
Hope this helps,
Morten
Sorry to bring up the subject again but is there anyway to tell whether an Entity within the SaveAndPublish event has just been created? For example, I'd like some code (using the new Page Id) to execute only when a page has first been created. The Entity passed within the Created event seems to have a Page Id of 0.
Thanks,
Dan.
You may perhaps check whether there exists more than one version of the page with your ID.
As morten has already written, please use the Saved event to gain access to the ID.
That's a good solution. Thanks Andreas.
Hi Dan,
you can also use the IRememberBeingDirty interface to check if an entity is new:
http://our.umbraco.org/documentation/Reference/Events-v6/determining-new-entity
Hope this helps,
Sören
Thats even better Sören. Thanks for your help.
Thats nice, didn't know about that either. Thanks!
is working on a reply...