Copied to clipboard

Flag this post as spam?

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


  • Anders Brännmark 226 posts 277 karma points
    Apr 25, 2013 @ 13:03
    Anders Brännmark
    0

    Copy a content structure via API, when new parent is created? (6.0.3)

    Using the new api with contentservice, I was going to copy a tree structure from a "system" folder in content to a newly created page.

    Hooked up the created event on contentservice. Using the contentservice.copy to copy an exsisting structure, problem is that that copy persists data before the e.Entity is persisted, meaning the e.Entity.Id that is needed for parent id on the copy method is null.

    The only way around this is to trigger an save of the e.Entity in the created event before attempting to copy a structure in under that entity.. This cant be the way to solve this....

    Whats the reason behind not persisting the entity before created event handler fire? Why cant we have a children collection on the IContent that would have allowed to add new children that havent been persisted to database using the same "transaction" as its parent?

     

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Apr 25, 2013 @ 15:10
    Morten Christensen
    0

    I would subscribe to the "Created" event on the ContentService, save the Content without firing events (which you can control) and then copy the pages.

    public class CopyAfterNewlyCreated : IApplicationStartupHandler
    {
      public CopyAfterNewlyCreated()
      {
        ContentService.Created += ContentService_Created;
      }
     
      public void ContentService_Created(IContentService sender, NewEventArgs<IContent> e)
      {
        sender.Save(content, 0, false);//Save without firing events
        sender.Copy(someContent, content.Id, false, 0); // 'someContent' being the content object you want to copy.
      }
    }

     

    The legacy Document class does a similar thing by saving the unsaved Content item without firing a save event for that operation and then it does some more legacy stuff.

    The ContentService.CreateContent method is only meant as a convenient way of creating a new content object based on the alias of a ContentType.

    - Morten

  • Anders Brännmark 226 posts 277 karma points
    Apr 25, 2013 @ 15:14
    Anders Brännmark
    0

    Yeah thats basiclly what I did. But it would have been nice that either the Content item is already persisted to db on the Created event, or that the content item or copy method could support this. Ie the content item could have a children collection that one could populate with a "clone" of the decendants from another content item. Or that copy could handle a not db stored parent reference.

     

Please Sign in or register to post replies

Write your reply to:

Draft