Constraints

It’s possible to use attributes to define which types of items can be created below another type, and who can create.

     [WithEditableName, WithEditableTitle]
     public abstract class PageBase : N2.ContentItem
     {
           [EditableFreeTextArea]
           public virtual string Text { get; set; }
     }

ItemAuthorizedRoles prevents this page from being created in the UI.

     [N2.PageDefinition]
     [RestrictParents(typeof(HomePage))] // allow sections immediately below home page
     [ItemAuthorizedRoles("Administrators")] // only admins can create this page
     public class SectionPage : PageBase
     {
     }

Restricting Parents and Children

RestrictParents make so this can only be created below a certain type. ItemAuthorizedRole restricts who can create it (but not delete, move or edit).

     [N2.PageDefinition]
     [RestrictChildren(typeof(NewsPage))] // prevent TextPage to be allowed
     public class NewsContainer : PageBase
     {
     }

RestrictChildren make so only the referenced types can be created below. By default all types are allowed.

     [N2.PageDefinition]
     [RestrictParents(typeof(NewsContainer))] // allow only below news container
     public class NewsPage : PageBase, ICommentable
     {
     }

Removing RestrictParents would make it possible to create this type below other types than NewsContainer.

     [N2.PageDefinition]
     [RestrictParents(typeof(PageBase))] // allowed below any page inheriting from PageBase
     public class TextPage : PageBase, ICommentable
     {
     }

RestrictParents can use a baseclass to restrict to a larger number of types.

     [PartDefinition]
     // allowed in any zone on any page
     public class FooterPart : ContentItem
     {
     }

Zone Restrictions

By default all parts are allowed in any zone.

It’s optional to define on the pages which zones are available. The benefit of declaring with [AvailableZone] is that parts can be added from the management UI without using drag and drop.

     public interface ICommentable
     {
     }

Polymorphic Restrictions

Base classes can use and be referenced for restrictions.

     [N2.PageDefinition]
     [ItemAuthorizedRoles(new string[0])] // allow no one
     public class HomePage : PageBase
     {
     }

Interfaces can also be referenced for restrictions.

     [PartDefinition]
     [RestrictParents(typeof(ICommentable))] // allowed only below commentable items
     [AllowedZones("Comments")] // allowed only in comments zone
     public class CommentPart : ContentItem
     {
     }

This part can only be created below types implementing ICommentable. Additionally only the zone “Comments” is allowed.

Enforced Sorting

To sort the children of a certain page the [SortChildren] attribute is used:

[PageDefinition("News Container")]
[SortChildren(SortBy.PublishedDescending)]
public class NewsContainer : ContentPageBase
{
}

Custom sort expression can be used to sort on other details:

[PageDefinition("Calendar")]
[SortChildren(SortBy.Expression, SortExpression = "EventDate")]
public class Calendar : ContentPageBase
{
}

Children are sorted whenever one child is saved. When applying the attribute to already saved content one page must be published again in order for changes to take effect.