November Happy Hour will be moved to Thursday December 5th.

Block versioning

Vote:
 

Hi everyone

I read this great post about the editorial cycle in CMS: http://world.episerver.com/blogs/Deane-Barker/Dates/2013/12/The-Editorial-Cycle-in-CMS-7/. It explains well how page versioning works but doesn't mention how block versioning works. Having a content ID, I would like to be able to get the latest version of both pages and blocks and then retrieve their "modified" date. With pages I can do it like this:

var contentRef = new ContentReference(contentId);
var versions = DataFactory.Instance.ListVersions(contentRef.ToPageReference());
var latestVersion = versions.OrderByDescending(x => x.Saved).FirstOrDefault();

With the latestVersion object I can then retrieve a PageData object and use the Saved property to display the modified date.

But what about blocks? DataFactory.Instance.ListVersions() needs a PageReference as parameter - so it only works for pages. And if I am somehow able to retrieve a BlockData object, there is no Saved property on this class, so it does not seem possible to get the modified date for blocks at all.

Can anyone explain whether is possible to retrieve different versions for block and get their modified date? Or explain why it is not?

#121575
May 14, 2015 12:13
Vote:
 

To retreive last modified date for the block you may add following tracking method for your block's base class:

public abstract class BaseBlockData : BlockData
{
    public DateTime LastModified
    {
        get
        {
            var changeTrackable = this as IChangeTrackable;
            return changeTrackable != null ? changeTrackable.Saved : DateTime.MinValue;
        }
    }
}
#121581
May 14, 2015 12:28
Vote:
 

The application I am building is a module which is going to be used on multiple EPiServer sites. The module will not define any block types itself and will only work with generic page and block data types - so I guess this solution will not work for me.

Any other ideas?

#121582
May 14, 2015 12:41
Vote:
 

You can create then extension method for any block type:

public static class BlockDataExtensions
{
    public static DateTime GetLastModified(this BlockData block)
    {
        var changeTrackable = block as IChangeTrackable;
        return changeTrackable != null ? changeTrackable.Saved : DateTime.MinValue;
     }
}
#121583
May 14, 2015 12:50
Vote:
 

Okay, I implemented this extension method and I am now able to get the modified date for a block. When I do...

var block = DataFactory.Instance.Get<BlockData>(contentRef);
var modified = block.GetLastModified();

... the modfied date is the date of the first version of the block. How do I get the latest version? That is, how do I do DataFactory.Instance.ListVersions() for blocks? Sorry if this is basic stuff, but I can't seem to find anything on this!

#121589
May 14, 2015 13:40
Vote:
 
var repo = ServiceLocator.Current.Get<IContentVersionRepository>();
var versions = repo.List(...);

This should give you all versions.

#121590
May 14, 2015 14:01
Vote:
 

Fantastic! Thank you!

Note that it is ServiceLocator.Current.GetInstance and not ServiceLocator.Current.Get

#121591
May 14, 2015 14:12
Vote:
 

Sorry for that, I was writting from top of my head. Will get compiler attached to my head in next humanoid version 2.0 ;)

#121592
May 14, 2015 14:14
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.