Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • lucuma 261 posts 563 karma points
    Feb 17, 2013 @ 19:49
    lucuma
    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?

    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);
                }
            }
    
        }
    }
  • Andreas Iseli 150 posts 427 karma points
    Feb 18, 2013 @ 15:46
    Andreas Iseli
    0

    This is a known bug and will be fixed with v6.0.1.

  • lucuma 261 posts 563 karma points
    Feb 19, 2013 @ 20:57
    lucuma
    0

    Thanks for the response..

  • Francisco 21 posts 72 karma points
    Apr 04, 2013 @ 00:45
    Francisco
    0

    I'm using v6.0.2 and it has not been fixed yet. Trying to find the issue on the issues.umbraco...

     

     

  • Michael Lawrence 128 posts 200 karma points
    May 03, 2013 @ 19:55
    Michael Lawrence
    0

    I'm using 6.0.5 and the issue still persists. Just an FYI.

  • KoosMos 19 posts 41 karma points
    May 22, 2013 @ 16:51
    KoosMos
    0

    When will the bug be fixed?

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    May 27, 2013 @ 12:24
    Morten Christensen
    1

    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:

    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.

    Hope this helps,

    Morten

  • Dan Lister 416 posts 1974 karma points c-trib
    Jun 18, 2014 @ 14:38
    Dan Lister
    0

    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. 

  • Andreas Iseli 150 posts 427 karma points
    Jun 18, 2014 @ 14:41
    Andreas Iseli
    1

    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.

  • Dan Lister 416 posts 1974 karma points c-trib
    Jun 18, 2014 @ 14:48
    Dan Lister
    0

    That's a good solution. Thanks Andreas.

  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 18, 2014 @ 14:54
    Sören Deger
    2

    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

  • Dan Lister 416 posts 1974 karma points c-trib
    Jun 18, 2014 @ 14:58
    Dan Lister
    0

    Thats even better Sören. Thanks for your help.

  • Andreas Iseli 150 posts 427 karma points
    Jun 18, 2014 @ 15:00
    Andreas Iseli
    0

    Thats nice, didn't know about that either. Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft