Get stored Content item to compare with Content item on saving / saved events (6.0.3)?
Trying to implement a check to see if a content item has ie changed name. Hooked into ContentService.Saving and Saved events. Item is not published, so no version exists. Thought that one could grab the item from the db again to compare whats stored and what the new "version" that comes with e.SavedEntities. Bummer, geting the item using same ContentService suing GetById only reflects that same "version" has the e.SavedEntities has.
So how do I get the previous values of a Content item? IsDirty and IsPropertyDirty doesnt work, as I need the old value and the new value.
I would assume you can use contentService.GetByVersion(id). When you are in the Saving event the new properties haven't been persisted yet and the Version hasn't been updated yet (whether the version number is actually updated depends on it being a SaveAndPublish or just Save and whether the previous version was Published). But either way within the Saving event use sender.GetByVersion(content.Version); this should give you the old values which you can then compare to the content passed into the Saving event.
Yea, but that shouldn't matter as the GetByVersion method is retreiving the previous (or well current) version directly from the db while you have the new values in memory. I haven't tested this, so I don't know for sure. But I'm assuming it will :)
Yes, I believe that should be possible because non of the new values has been persisted to the db yet. The transaction to persist is started after the Saving event.
Get stored Content item to compare with Content item on saving / saved events (6.0.3)?
Trying to implement a check to see if a content item has ie changed name. Hooked into ContentService.Saving and Saved events. Item is not published, so no version exists. Thought that one could grab the item from the db again to compare whats stored and what the new "version" that comes with e.SavedEntities. Bummer, geting the item using same ContentService suing GetById only reflects that same "version" has the e.SavedEntities has.
So how do I get the previous values of a Content item? IsDirty and IsPropertyDirty doesnt work, as I need the old value and the new value.
/Anders
I would assume you can use contentService.GetByVersion(id). When you are in the Saving event the new properties haven't been persisted yet and the Version hasn't been updated yet (whether the version number is actually updated depends on it being a SaveAndPublish or just Save and whether the previous version was Published). But either way within the Saving event use sender.GetByVersion(content.Version); this should give you the old values which you can then compare to the content passed into the Saving event.
Makes sense?
- Morten
Hmm, will check but I think you dont have any other version if the item is not yet published.
/Anders
Yea, but that shouldn't matter as the GetByVersion method is retreiving the previous (or well current) version directly from the db while you have the new values in memory.
I haven't tested this, so I don't know for sure. But I'm assuming it will :)
- Morten
Ok, so I can get the "same" version but from db directly?
Yes, I believe that should be possible because non of the new values has been persisted to the db yet. The transaction to persist is started after the Saving event.
Ok, Thanks!
Thanks Morten, just what I needed :-)
is working on a reply...