Get all the instances of a block type

Vote:
 

I am having some problems with the block types and wondering if anyone 

have any solution for getting all the instances of a blocktype?

 

//André

#63793
Nov 29, 2012 15:59
Vote:
 
Doesn't Johans loading multiple instaces work?
IEnumerable<ContentReference> references = GetSomeReferences();
IEnumerable<IContent> items = Locate.ContentRepository().GetItems<IContent>(references, LanguageSelector.AutoDetect());

 

#63796
Nov 29, 2012 16:27
Vote:
 

Here is a code example if you mean looking for the content of certain type:

var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var contentModelUsage = ServiceLocator.Current.GetInstance<IContentModelUsage>();

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

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

// TODO: do the stuff with usages

    

#63814
Nov 30, 2012 9:27
Vote:
 

Thank for the answers!



This will get me each usage of the created block, but i would like to get

a list of the available shared blocks no matter on which paghe the block is used.

 

For example, when im using contactblocks i would like to make a list of all 

published contactblocks and get the information from the blocks that i will then use 

to present on another page. Also, is it possible to somehow cast a contentusage to its actual blocktype

or do i have to get that content by using the contentreference ID from the contentusage object.

 

Thanks in advanced!

#63821
Nov 30, 2012 11:27
Vote:
 

usage.ContentLink is the reference to block instance, so I guess this is what you need.

#63822
Nov 30, 2012 11:33
Vote:
 

I will borrow this thread.

If I iterate on the usages in Dmytros example I thought I would get only the published instances of my block type. But it seems that I get all versions. 

In my case I populate a dropdown in an selectionfactory (I use the contentlink as value in my dropdown.). In another block where I use the dropdown I would like to choose the current version of a specific block instance for getting som data.

But now every time I publish my block containing the data I want to use I get another version in the dropdown. And to get the latest version in my "consuming" block I need to go into forms mode an choose the latest version which I don't want to do.

In my consuming block I want to choose among blocks of my blocktype by name and be able to always load the latest version.

How can I accomplish what I want.

#75131
Sep 18, 2013 13:29
Vote:
 

I solved it. On the usage you can do like this ContentLink.CreateReferenceWithoutVersion() to get the correct reference.

#75138
Sep 18, 2013 14:37
Vote:
 

Thanks Per. However

ContentLink.CreateReferenceWithoutVersion()

is obselete. Use this instead

ContentLink.ToReferenceWithoutVersion()
#179753
Jun 20, 2017 17:59
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.