Working programatically with shared blocks




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 ( ) 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? 

Nov 30, 2012 12:57

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.

Nov 30, 2012 13:23

Thank you Johan.

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.