Copied to clipboard

Flag this post as spam?

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


  • Joshua Walsh 30 posts 151 karma points
    Aug 10, 2015 @ 06:10
    Joshua Walsh
    0

    Saving a document from within its Published event

    Hi,

    I have a case where I need to update a property on a document immediately after it is published. The idea is that on a publish event the unpublished value will be published and then a new unpublished value will instantly be created.

    For example: (Expected)

    Published property value: 5

    Unpublished property value: 6

    [User presses Save&Publish]

    Published property value: 6

    Unpublished property value: 7

    I'm using the ContentService.Published event to do this: (Note that this isn't my actual use-case, this is a Minimal, Complete and Verifiable example)

    void ContentService_Published(IPublishingStrategy sender, PublishEventArgs<IContent> e)
    {
        foreach (var newDocument in e.PublishedEntities)
        {
            var originalValue = newDocument.GetValue<int>("exampleProperty");
    
            newDocument.SetValue("exampleProperty", originalValue + 1);
    
            ApplicationContext.Current.Services.ContentService.Save(newDocument);
        }
    }
    

    However it seems that calling ContentService.Save() on a document in the Published event cancels the publish on that node. This behaviour is surprising, is there a way around this? I would expect the publish to continue and a new (draft) version of the document to be created chronologically after the published one.

    So to complement my expected example earlier, here is the actual behaviour:

    Published property value: 5

    Unpublished property value: 6

    [User presses Save&Publish]

    Published property value: 5

    Unpublished property value: 7

    Thanks,

    Josh

  • Joshua Walsh 30 posts 151 karma points
    Aug 13, 2015 @ 02:20
    Joshua Walsh
    100

    I don't know of a real way around this, but I eventually came up with a workaround which is a custom property editor for this property. Whenever a user edits or publishes a content node the property editor's controller calls an API which checks if the published value == the draft value, and if so it bumps the draft value. It's not particularly pretty but it works reliably.

Please Sign in or register to post replies

Write your reply to:

Draft