Copied to clipboard

Flag this post as spam?

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


  • greengiant83 88 posts 109 karma points
    May 19, 2011 @ 00:02
    greengiant83
    0

    Publishing from the AfterPublish event yields strange results

    Given a certain type of document, I want to publish its children once it itself is published.  So I made class that inherits from ApplicationBase and wire up and event to Documet.AfterPublish from there I execute this code:

     

    void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                if (sender.ParentId == templateStructuresNodeId) return;
                var templateNode = TemplateStructures.Children.SingleOrDefault(i => i.ContentType.Alias == sender.ContentType.Alias);
                if (templateNode != null)
                {
                    foreach (var child in sender.Children)
                    {
                        if (!child.Published)
                        {
                            child.Publish(User.GetCurrent());
                            umbraco.library.UpdateDocumentCache(child.Id);
                        }
                    }
                }
            }

    The problem is that once this code runs the children appear to be published, but if you examine them int he back office the Link to Document is listed as #  If I take the publishing code and just run it as a the result of a button click on a test page everything works fine.  The child items are published and their link url's are listed correctly.  What could be the problem?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 19, 2011 @ 04:49
    Tom Fulton
    0

    Hi,

    I once ran into a similar issue - to fix, try using umbraco.content.AfterUpdateDocumentCache  instead of Document.AfterPublish.  I think the document is not fully published in AfterPublish - it's not added to the cache yet, so the children have trouble publishing if you call them from here.  Here's a thread that explains it a bit

    Hope this helps,
    Tom

Please Sign in or register to post replies

Write your reply to:

Draft