Try our conversational search powered by Generative AI!

Suspicious cast there is no type in the solution which is inherited from both 'BlockData' and 'IContent'


Hi all,

the code below causes "Suspicious Cast" warning. Nevertheless everything works.

if(contentRepository.TryGet<FormContainerBlock>(copyContentEventArgs.ContentLink, out var formContainerBlock))
    var writableFormContainerBlock = formContainerBlock.CreateWritableClone() as IContent;


    contentRepository.Save(writableFormContainerBlock, SaveAction.Publish);


The point is that without "as IContent" part, I could not call "Save" since it's requiring first parameter to be "IContent". And if I put "IContent" in "TryGet<>" instead of "FormContainerBlock", I then can't call "CreateWritableClone()" method.

Edited, Jan 31, 2019 16:59

Hi Fujio

This is correct as per the documentation located here:

You could also do this when calling save :

contentRepository.Save((IContent)writableFormContainerBlock, SaveAction.Publish);



Jan 31, 2019 17:47

Sorry for such a general newbie question and thanks! Will play around with it.

Jan 31, 2019 18:10

Fujio, don't be apologising. We were all newbies once and by asking questions we only learn, in fact ask more questions I would say. 

Jan 31, 2019 18:33

Thanks Paul!

After taking a look at this explanations - - if I get it correctly, another question appeared - is it safe to "transform" FormContainerBlock into IContent? Will FormContainerBlock after casting into IContent get back into form of FormContainerBlock (let's say after "Save")? Won't FormContainerBlock lack some (properties?, etc.) as IContent?

PS.: I also cast "ElementBlockBase" (of that "FormContainerBlock") to "ICotnent" inside that code the same way as casting "FormContainerBlock" to "IContent".

Edited, Feb 01, 2019 11:55

Hi Fujio,

Everything you have said makes sense to me, so i had to have a look further.

Digging into the FormContainerBlock (in Episerver.Forms) it does not implement IContent.

So if I was trying to create a FormContainerBlock programmatically I would use this method:

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var languageSelectorFactory = ServiceLocator.Current.GetInstance<LanguageSelectorFactory>();

var blockType = contentTypeRepository.Load<FormContainerBlock>();
var block = contentRepository.GetDefault<IContent>EPiServer.Forms.Configuration.Settings.Current.RootFolder, blockType.ID, languageSelectorFactory.Create(ContentLanguage.PreferredCulture.Name).Language);
block.Name = "My new form conatiner block";
contentRepository.Save(block, SaveAction.Publish, AccessLevel.NoAccess);

Hope this helps.


Feb 03, 2019 22:16
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.