Quantcast
Viewing all articles
Browse latest Browse all 9076

Get all instances of a certain block type in EPiServer 8

I may have missed the deep dive lesson on the EPiServer repository API's and figured there should be a simple way to retreive all blocks of a certain type that are published. But no, there seem to be no such thing and the other examples on this forum either perform horribly or are bad implementations that doesn't consider multiple versions, publish status and so on.

This is my routine at the moment, does anyone have any advice to make this a bit cleaner?

// loading a block type
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var blockType = contentTypeRepository.Load<ContactBlock>();
// get usages, this also includes versions
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();
var usages = contentModelUsage.ListContentOfContentType(blockType);
var repository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contactBlocks =
    usages.Select(u => repository.Get<IContent>(u.ContentLink))
        .Where(x =>
            // ...filter published
            (x as IVersionable).Status.Equals(VersionStatus.Published)
            // remove deleted&& !x.IsDeleted)
        .FilterForVisitor()
        .Cast<ContactBlock>();



Thanks


Viewing all articles
Browse latest Browse all 9076

Trending Articles