Copied to clipboard

Flag this post as spam?

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


  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Oct 28, 2010 @ 18:29
    Hendy Racher
    1

    Automatically create and publish a child node

    Hi

    I'm trying to automatically create and publish a child node when a particular parentnode type is published. For example in this case the child node is of type 'comments' and the url expected is /parentnode/comments.aspx

    The trouble I'm having is that the url of the child node comments is being set to #. If I then publish the comments node manually it's fixed, but this isn't ideal.

    class CreateRecipeCommentsAndRatingsNode : ApplicationBase
    {
    public CreateRecipeCommentsAndRatingsNode()
    {
    Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);
    }

    void Document_AfterPublish(Document sender, PublishEventArgs e)
    {
    if (sender.ContentType.Alias == "Recipe")
    {
    if (!sender.HasChildren)
    {
    string recipeName = sender.Text;
    User currentUser = User.GetCurrent();

    Log.Add(LogTypes.Custom, sender.Id, "Creating RecipeCommentsAndRatings node for " + recipeName);

    Document recipeCommentsAndRatings = Document.MakeNew("Comments",
    DocumentType.GetByAlias("RecipeCommentsAndRatings"),
    currentUser,
    sender.Id);

    //recipeCommentsAndRatings.Save();
    recipeCommentsAndRatings.Publish(currentUser);
    //library.UpdateDocumentCache(recipeCommentsAndRatings.Id);
    }
    }
    }
    }
  • Rich Green 2246 posts 4008 karma points
    Oct 28, 2010 @ 18:33
    Rich Green
    0

    You have to also push it to the XML

     //preparing for publishing, this actually puts this node and xml into the database
                recipeCommentsAndRatings.Publish(currentUser));

    So add this line

                //taking the prepared xml, and push it to the website
                umbraco.library.UpdateDocumentCache(recipeCommentsAndRatings.Id);

    Rich

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Oct 28, 2010 @ 18:34
    Hendy Racher
    0

    Hi Rich,

    Tried that, no joy unfortunately.

    Thanks,

    Hendy

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Oct 28, 2010 @ 18:39
    Hendy Racher
    0

    Doh, lost my edits from the original post - I suspect it's because the node is being created and published in the after publish event of the parent, but it'd be nice to be able to create and publish a child node on first publish of the parent. I'm going to try and create the child node after save of the parent, but suspect as this is earler in process it may not work. Any advice appreciated,

    Thanks,

    Hendy

     

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Oct 29, 2010 @ 11:59
    Hendy Racher
    1

    Hi,

    I found a solution, by moving the publish of the child node out from the parent Document_AfterPublish, into the parent Content_AfterUpdateDocumentCache event which happens later in the execution process.


    Cheers,

    Hendy

     

  • lucuma 261 posts 563 karma points
    Mar 22, 2012 @ 20:23
    lucuma
    0

    I just ran into this same issue on 4.7.1.   In my case, in the after save event I was creating and publishing some child/grandchild nodes before the parent node was published just to make it easier on the user since it was data entry type stuff.  So, after the user published the parent (grand parent in this case) node, the child nodes although had the publish status never showed up in the decendants.where("visible") queries.  

    This is what I did when creating the nodes and it didn't work:

     ib.Publish(author);

     umbraco.library.UpdateDocumentCache(ib.Id); // this seems to have no effect, the main parent node in this case is still not published.

    The only way that these nodes would show up after their ancestor was published was to do a refreshcontent in the after publish event when the ancestor was published.

    Is this a bug or by design?

Please Sign in or register to post replies

Write your reply to:

Draft