Don't miss out Virtual Happy Hour this Friday (April 26).

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'

Vote:
 

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.

#200968
Edited, Jan 31, 2019 16:59
Vote:
 

Hi Fujio

This is correct as per the documentation located here:

https://world.episerver.com/documentation/class-library/?documentId=cms/7/0b43644e-7c61-d035-3f70-1ab53854e4d3

You could also do this when calling save :

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

Thanks

Paul

#200972
Jan 31, 2019 17:47
Vote:
 

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

#200973
Jan 31, 2019 18:10
Vote:
 

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. 

#200975
Jan 31, 2019 18:33
Vote:
 

Thanks Paul!

After taking a look at this explanations - https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/casting-and-type-conversions - 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".

#201001
Edited, Feb 01, 2019 11:55
Vote:
 

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.

Paul

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