Copied to clipboard

Flag this post as spam?

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


  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 18, 2014 @ 12:57
    Dan Diplo
    0

    ContentService GetById vs GetPublishedVersion

    In the ContentService API there are two methods:

    .GetById(int id)

    and

    .GetPublishedVersion(int id)

    I would have expected that for a node Id that has (just) been published that both would return the exact same IContent item. Is my expectation correct?

    Because I've seen situations where the item returned by GetById returns the previous version, rather than the current version. However, GetPublishedVersion does return the latest version. This occurs within a custom event (PageCacheRefresher_CacheUpdated) event.

    Is this expected behaviour? Are their any rules on which method you should use in a given circumstance?

     

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Feb 18, 2014 @ 15:53
    Morten Christensen
    100

    Hi Dan,

    Your expectation is correct, but not what you are seeing (as I read it is not).

    The GetById method always returns the latest version of a content item (regardless of that item being published or not). There will never be a version "newer" then what GetById returns.

     

    The GetPublishedVersion method should always return the latest PUBLISHED version. But not necessarily the latest version. A content item that is saved after being published will have a newer version then GetPublishedVersion.

    As far as I remember there should only be one published version regardless of how many versions has previously been published, but i'd have to double check as it was one of the things we struggled with from v4 where the versioning was a bit different.

     

    - Morten

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 18, 2014 @ 16:18
    Dan Diplo
    0

    Thanks, Morten. Your explanation fits with what I expected. I think the problem I'm seeing is that straight after a publish GetById(int) is returning the values of the node before it was published but GetPublishedVersion(int) is returning the correct after-publication values. It may be something to do with how events are raised in Umbraco, I'm not sure. It's not a big deal as I can work around, just curious I was understanding correctly. Thanks for your help.

  • Morten Christensen 596 posts 2773 karma points admin hq c-trib
    Feb 18, 2014 @ 16:21
    Morten Christensen
    1

    Hmm, could be a caching issue. GetById looks in cache whereas GetPublishedVersions goes straight to the database.

    Any chance I could get you to log an issue for it?

    - Morten

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Feb 18, 2014 @ 16:55
    Dan Diplo
    0

    Sure, no problem - http://issues.umbraco.org/issue/U4-4254 ;

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft