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

Getting Blocks Programatically By Type

Vote:
 

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
Dave

#74598
Sep 02, 2013 17:28
Vote:
 

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

Frederik  

 

#74614
Sep 02, 2013 21:32
Vote:
 

You could use IContentModelUsage.ListContentOfContentType(ContentType contentType). 

#74621
Sep 03, 2013 8:03
Vote:
 

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

#74622
Sep 03, 2013 8:05
Vote:
 

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
Dave

#74641
Sep 03, 2013 12:58
Vote:
 

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

#74642
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.