Copied to clipboard

Flag this post as spam?

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


  • Jesper Hauge 298 posts 487 karma points c-trib
    Mar 31, 2011 @ 11:36
    Jesper Hauge
    0

    Using document.Published in an event handler

    I'm trying to build an event handler that moves a document to a date specifik folder when saving and publishing the document for the first time. I assumed the Published property on the document would be true, when the document has been published once.

    I'm using the following code:

     private void Document_BeforeSave(Document sender, SaveEventArgs e)
     {
      if (sender.ContentType.Alias == "Cupcake" && !sender.Published) HandleCupcakeBeforeSave(sender, e);
     }

    But it seems sender.Published is false even when the document being saved is published. Is there something I am missing?

    Regards
    Jesper Hauge

  • Laurent Lequenne 123 posts 248 karma points
    Mar 31, 2011 @ 11:41
    Laurent Lequenne
    0

    Saving is not Publishing !

  • Jesper Hauge 298 posts 487 karma points c-trib
    Mar 31, 2011 @ 11:49
    Jesper Hauge
    0

    @Laurent:

    I'm aware of that! Publishing occurs later.

    What I'm trying to to is prevent HandleCupcakeBeforeSave from running after the document in question has been published elsewhere. If I don't do that the method will run on every save and publish (since publishing alse triggers a save), thereby moving the document to the current date.

    If sender.Published was true when the document being saved is already published, I could use this property to prevent moving already published documents.

    .Jesper

     

  • Laurent Lequenne 123 posts 248 karma points
    Mar 31, 2011 @ 12:09
    Laurent Lequenne
    0

    insteed of using the save or publish handler, you can use the document_xxxnew handler, so that way you are sure that the document will be moved only once at the creation.

  • Jesper Hauge 298 posts 487 karma points c-trib
    Mar 31, 2011 @ 12:15
    Jesper Hauge
    0

    D'oh - I forgot to check for an event like that - that will do the trick. Thanks Laurent.

    But I still find it a bit odd that the sender.Published property is always false ...

    .Jesper

  • Laurent Lequenne 123 posts 248 karma points
    Mar 31, 2011 @ 12:36
    Laurent Lequenne
    0

    Yah ! No idea, I didn't worked with this property yet... Try with doc.HasPublishedVersion() ? Maybe the Published setting is only valid for the current document verson, in that way, it could be logic that if you edit and Save. The Published property is set to false, as the current version of the document need to be published before it is visible on the frontend.

    On the other hand in the eventhandler beforeSave the published property could not be set to false as it is not yet saved. The published property should be set to false afterSave. So maybe the published is set somewhere between when you push the edit button and the beforesave handler... But this can only be told by a such umbraco guru that is definitely not hanging on the umbraco forums :-)

     

Please Sign in or register to post replies

Write your reply to:

Draft