Search code examples
umbracoumbraco7

How do I check if a page or node (IPublishedContent) uses a specific inherited document type?


I have a document type Page which inherits from a parent document type Page Base.

On a rendered page, I can see if the document type is equal to the Page document type alias:

// Where Model is type Umbraco.Web.Models.RenderModel.
if (Model.Content.DocumentTypeAlias == "page")
{
    // My current page uses the "page" document type.
}

But how can I check to see if my IPublishedContent inherits from Page Base?


Solution

  • Turns out there is a method for this on the IPublishedContent type:

    // Where Model is type Umbraco.Web.Models.RenderModel.
    if (Model.Content.IsDocumentType("pageBase", true))
    {
        // My current page uses the "pageBase" document type.
    }
    

    The overload on .IsDocumentType() which accepts a boolean is called recursive and it checks to see if your IPublishedContent either is the supplied document type, or is derived from that document type.

    Update: However!!

    There is a bug using recursive .IsDocumentType() if your document types are stored in folders. This issue has been logged here.

    Instead, you can use .IsComposedOf():

    // Where Model is type Umbraco.Web.Models.RenderModel.
    if (Model.Content.IsComposedOf("pageBase"))
    {
        // My current page uses the "pageBase" document type.
    }
    

    This method both checks document type compositions and inheritance.

    This method differs from recursive .IsDocumentType() because it only checks inheritance/compositions, not if the current doc type is the provided alias.