Try our conversational search powered by Generative AI!

Getting Blocks Programatically By Type


Hi All

Scattered throughout the site I am working on there are lots of videos, I have created a custom block for adding these. As part of the media area of the site though I need to pull in all of the videos, I want to do this programatically rather than relying on the users to add them all to a ContentArea, is there a way of programatically getting all blocks of a particular type like you would do for pages?

Many thanks

Sep 02, 2013 17:28

If you had EPiServer Find I would recommend using that to find them, if you have to use the built in API I would try with IContentLoader's GetDescendant() method.

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
contentLoader.GetDescendents(new ContentReference(blockFolderId)).Select(pageReference => contentLoader.Get<BlockData>(pageReference)).Where(block => block is MyBlockType)

Note that depending on how many blocks you have this can be quite a heavy operation.

Hope this helps



Sep 02, 2013 21:32

You could use IContentModelUsage.ListContentOfContentType(ContentType contentType). 

Sep 03, 2013 8:03

Forgot to mention that the component that implements the interface can be retrieved from IOC container.

Sep 03, 2013 8:05

Hi Guys

Thank you very much for your response, On your comment Johan, is there a method to get the ContentType either by a Guid or Sytem.Type to pass to the ListContentOfContentType?

Many Thanks

Sep 03, 2013 12:58

There is a method Load on IContentTypeRepository that takes a type as parameter and returns a ContentType

Sep 03, 2013 12: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.
* 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.