Try our conversational search powered by Generative AI!

How to check if a Block is published or not

Vote:
 

Hi Guys,

i'm fetching all blocks in my cms (across all pages) like so:

var contentTypeRepository = ServiceLocator.Current.GetInstance();
var contentModelUsage = ServiceLocator.Current.GetInstance();

// loading a block type
var blockType = contentTypeRepository.Load();

// getting the list of block type usages, each usage object has properties ContentLink, LanguageBranch and Name
var usages = contentModelUsage.ListContentOfContentType(blockType);


Then i loop through all my usages and fetch the block type (CreationBlock) which are speciallike so:

var contentLoader = ServiceLocator.Current.GetInstance();
        List result = new List();
        foreach(var usage in usages)
        {
            var block = contentLoader.Get(usage.ContentLink.CreateReferenceWithoutVersion());

                result.Add(block);
            
        }

However in my Result i have now published and unpublished blocks... 

First: is the modelUsages repo thing the best way to fetch all blocks from the episerver database of contenttype CreationBlock?

Second: and if so, how can i then filter only the published ones?

Thanks in advance!!

#149878
Jun 09, 2016 14:34
Vote:
 

1. There are a few ways. Either use GetDescendents on content loader or your way or using search engine like find. For small sites it doesn't matter much as long as you cache result. On larger sites, Find will work best...model usages as shared second best.

2. Use the classes below Episerver.Filters like 

new FilterPublished().Filter(blocksToFilter);
#149953
Edited, Jun 09, 2016 20:13
Vote:
 

Thanks Daniel,

but i found other threads with similar probelms as mine like this:

http://world.episerver.com/forum/developer-forum/Developer-to-developer/Thread-Container/2015/6/get-all-instances-of-a-certain-block-type-in-episerver-8/

As most of the repo's epi provides are already cached i'm not concerned about the performance... 

Thanks for your insight as well... 

#149968
Jun 10, 2016 8:10
Vote:
 

As long as you have small to medium sites it will not be a problem. Might be worth making it possible to log the execution time with log4net though to be sure. 

ListContentOfContentType is not cached btw so will always result in a database call. 

I would still say it's the best way if you don't have Episerver Find. But since it will make a database call every time it will be fairly slow and will benefit from caching...

You are right though that ContentRepository calls are usually cached (like GetChildren etc). So caching that a second time will usually be a waste of typing... :)

#149974
Edited, Jun 10, 2016 9:49
* 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.