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?
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.
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.
ContentService GetById vs GetPublishedVersion
In the ContentService API there are two methods:
and
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?
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
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.
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
Sure, no problem - http://issues.umbraco.org/issue/U4-4254 ;
Thanks!
is working on a reply...