Copied to clipboard

Flag this post as spam?

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


  • Joseph 59 posts 140 karma points
    Dec 18, 2013 @ 23:34
    Joseph
    0

    Issues hooking onto Umbraco 6.1's PublishingStrategy

    I'm currently working on a 'Link to Origin' feature which allows content to be shared across different sections i.e. by creating the content node once and then creating lots of link nodes (which is essentially a content picker). One component of this would be to synchronize publishing e.g. when the content node is unpublished, all link nodes should be unpublished; when the content node is not published, the link node should not be publishable. To do this, I've written some publish event handlers and while I have verified that they fire; they seem to be ineffective:

    PublishingStrategy.Publishing += PreventPublishingOfLinkToObjectIfOriginUnpublished;

    void PreventPublishingOfLinkToObjectIfOriginUnpublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        foreach (var item in e.PublishedEntities)
        {
            if (item.ContentType.Alias == "LinkToOrigin")
            {
                var origin = ApplicationContext.Current.Services.ContentService.GetById(item.GetValue<int>("originPage"));
                if (!origin.Published)
                {
                    e.Cancel = true;
                }
            }
        }
    }
    

    This seems to work i.e. when the publish event is intercepted and cancelled when the origin node is found to not be published.

    PublishingStrategy.UnPublishing += PreventUnpublishingOfLinkToObjectIfOriginPublished;

    void PreventUnpublishingOfLinkToObjectIfOriginPublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        foreach (var item in e.PublishedEntities)
        {
            if (item.ContentType.Alias == "LinkToOrigin")
            {
                var origin = ApplicationContext.Current.Services.ContentService.GetById(item.GetValue<int>("originPage"));
                if (origin.Published)
                {
                    e.Cancel = true;
                }
            }
        }
    }
    

    Similar code as above but it basically prevents a link node from being unpublished if its origin is published (this is a customer requirement). This however, doesn't work even though the code steps through correctly (in the debugger as expected). The backoffice UI behaves somewhat strangely as well which suggests to me that it could be a bug: I get the prompt telling me that the unpublish was successful (even though e.Cancel was set to true); the label for the node on the tree is in solid font (which indicates that it's published) but Publication Status and Link to document reads "This item is not published". If I refresh the page, the node looks exactly like it's published i.e. Publication Status and Link to document reverts back.

    PublishingStrategy.Published += PublishAllLinks;

    PublishingStrategy.UnPublished += UnpublishAllLinks;

    void UnpublishAllLinks(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        foreach (var item in e.PublishedEntities)
        {
            var linksToOrigin = GetLinksTo(item).ToList();
            if (linksToOrigin.Any())
            {
                foreach (var link in linksToOrigin)
                {
                    var success = sender.UnPublish(link, User.GetCurrent().Id);
                }
                library.RefreshContent();
            }
        }
    }
    
    void PublishAllLinks(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        foreach (var item in e.PublishedEntities)
        {
            var linksToOrigin = GetLinksTo(item).ToList();
            if (linksToOrigin.Any())
            {
                foreach (var link in linksToOrigin)
                {
                    var success = sender.Publish(link, User.GetCurrent().Id);
                }
                library.RefreshContent();
            }
        }
    }
    

    I also wrote the handlers above which essentially synchronizes links when a content node is published or unpublished. Stepping through the code in a debugger, it seems to be doing what it's meant to be doing but the link nodes are not actually published/unpublished.

    Am I missing something here or are the PublishingStrategy event hooks not meant to be used in this way? NB: this sort of functionality was implemented in Umbraco 4 without any apparent issues.

  • Joseph 59 posts 140 karma points
    Jan 08, 2014 @ 03:04
    Joseph
    0

    I'm still having issues with this - can anyone help please?

Please Sign in or register to post replies

Write your reply to:

Draft