Try our conversational search powered by Generative AI!

Working programatically with shared blocks

Vote:
 

Hi,

 

In my current project we are importing a bunch of data, where some of it will be built into shared blocks. I found this blogpost ( http://joelabrahamsson.com/entry/working-programmatically-with-local-blocks-in-episerver-7 ) from Joel but this deals with Local blocks.

Should we implement IContent for the Shared Block to be able to store it using IContentRepository.Save? 

#63828
Nov 30, 2012 12:57
Vote:
 

The main operations for shared block instances are done through IContentRepository interface. To get a new instance of a shared block you call method GetDefault<T> where T is your block type. Note here that the instance you get back from GetDefault<T> will implement IContent even if your block type doesn't. What happens runtime is that when a shared block instance is created EPiServer will create a new type inheriting your block type, this new subclass will implement IContent.

Then after assigning values to your instance (note that you need to cast it to IContent to be able to set e.g. Name) you can persist the shared block instance by calling IContentRepository.Save (here you also need to cast it to IContent).

And you can then load the persisted instance by calling Get<T> with the reference you got back from Save.

#63829
Nov 30, 2012 13:23
Vote:
 

Thank you Johan.

#63830
Nov 30, 2012 13:46
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.