Optimizely: Page type, Block type and Content area restrictions

As CMS or Commerce developers we work a lot with page types, block types and different in-built properties like content area etc. Sometimes we just create our Page Type, Block Types and properties and a lot of times we need to add restrictions such as what block types are allowed in this content area. Which specific blocks are not allowed in this content area and Sometimes which block/page can only be used under certain types or permission of users.

All these scenarios are discussed in Optimizely forums and in blogs. I just want to put them together and create a cheat sheet for my convenience and hopefully other benefits from it as well.

Let’s discuss different scenarios

  • Restrict block type or page to a be created by certain user/group
  • Restrict page type allowed under certain page types in content tree
  • Content area: Restrict all block types and allow a few block types
  • Content area: Restrict a few block types and allow rest of block types
  • Restrict block in all conntent areas

Restrict page type allowed under certain page types under content tree

The following examples only allow specific page types to be added under a page in the content tree.

For example, you have created a parent settings page that holds all website setting pages. Or a Blog Home page that can only hold article pages etc

[ContentType(
        DisplayName = "Settings Page",
        GUID = "4c453b87-eca9-4766-a5b7-ff2bfc6d3922", 
        Description = "All site-wide settings.",
        GroupName = Constants.StringConstants.PageTypes.SettingsPages)]
    [AvailableContentTypes(Include = new[] {
       typeof(EmailSettingsPage),
        typeof(BlogSettingsPage),
        typeof(UserAccountSettingsPage),
        
    })]
    
    public class SettingsPage : PageData
    {

    }

Restrict block type or page to a be created by certain user/group

Taking the above example if you want the Setting page to be created by users with Administrator permissions

    [ContentType(
        DisplayName = "Settings Page",
        GUID = "4c453b87-eca9-4766-a5b7-ff2bfc6d3922", 
        Description = "All site-wide settings.",
        GroupName = Constants.StringConstants.PageTypes.SettingsPages), Access(Roles = "Administrators")]

You can add similar settings in blocks as well

[ContentType(DisplayName = "Editorial Block", GUID = "0d881c79-2a10-4be3-8792-bee335cc7cf7", Description = "Editorial block"), Access(Roles = "Administrators") ]
    public class EditorialBlock : EditorialBaseBlock

Content area: Restrict all block types and allow a few block types

Following code only allow Page type of Standard Page in the content area

[AllowedTypes(AllowedTypes = new [] { typeof(StandardPage) })]
public virtual ContentArea RelatedContentArea { get; set; }

Following code only allow Page type of Standard Page and Block type of EditorialBlock in the content area

[AllowedTypes(new [] {typeof(StandardPage), typeof(EditorialBlock)})]  public virtual ContentArea RelatedContentArea { get; set; } 

Content area: Restrict a few block types and allow the rest of the block types

Following code restrict EditorialBlock but allow all other page/block type

[AllowedTypes(RestrictedTypes = new [] { typeof(EditorialBlock) })]
public virtual ContentArea RelatedContentArea { get; set; }

Following code allow Standard page and restrict EditorialBlock

[AllowedTypes(AllowedTypes = new [] { typeof(StandardPage) }, RestrictedTypes = new [] { typeof(EditorialBlock) })]
public virtual ContentArea RelatedContentArea { get; set; }

Restrict a block in all content areas

A lot of times we create a block but we do not want the content editor to use it. We probably auto-generate instances this block instances or we might want to obsolete this block in future.

[ContentType(DisplayName = "Editorial Block", AvailableInEditMode =false, GUID = "0d881c79-2a10-4be3-8792-bee335cc7cf7", Description = "Editorial block")]
    public class EditorialBlock : BlockData
    {
      .......
      }

About the author

Naveed Ul-Haq

I am a UK based technical architect. I love working with .NET based CMS, eCommerce solutions, .NET Core, DevOps, and Cloud computing. I am a Certified Episerver CMS developer, MCSD (Microsoft Certified Solution Developer) and MCP in Azure application development. I spend my free time with my family and reading books. You can contact me on [email protected]

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *