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

IContent - I don't have an issue but I understand why.

Vote:
 

Hi all,

Why does this works ?

[ContentType(DisplayName = "MyBlock ", GUID = "eccd619a-827c-4b13-b0c8-28c671f0f949", Description = "")]
public class MyBlock : BlockData
    {
        [Ignore]
        public virtual string CmsBlockName
        {
            get
            {
                return (this as IContent).Name;
            }
        }
    }

I looked everywhere and none of the ancestors of MyBlock implements IContent. So why am I able to get the name on the content block by doing this ?

#193971
Jun 08, 2018 18:11
Vote:
 

All shared blocks will implement the IContent interface at runtime.
Local blocks won't.

If you call CmsBlockName on a local block, you'll get null reference exception.

I would suggest this code instead:

public static class BlockHelpers
{
    public static string GetName(this BlockData block)
    {
        var content = block as IContent;
        return content?.Name;
    }
}
#193972
Edited, Jun 08, 2018 18:21
Vote:
 

Good to know! Thank you.

#193974
Jun 08, 2018 18:25
Vote:
 

As Dejan points out is blocks a special kind of content in meaning they can both be a property on other content (local blocks) or be an own instance (shared block). Here is a post that describes how shared blocks are built up.

#193993
Jun 11, 2018 9:20
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.