Multi-level site headers

The objective is to handle headers for a main site and several smaller micro sites. Each microsite will have multiple pages which should display the micro-site header. Unpublished content should not be displayed


[ChildActionOnly]
public PartialViewResult Header()
{
    //AncestorOrSelf in IContent
    try
    {
        var pageId = UmbracoContext.PageId;
        var item = Services.ContentService.GetById(pageId.Value);

        //Check current Item for "Site Components" folder.
        var siteComp = item.Children()
            .FirstOrDefault(m => m.Published 
                && m.ContentType.Alias == "folderSiteComponents");

        //If current item does not have it, then search ancestors
        if (siteComp == null)
        {
            siteComp = item.Ancestors()
            .Select(m => m.Children()
                .FirstOrDefault(n => n.Published 
                    && n.ContentType.Alias == "folderSiteComponents"))
            .LastOrDefault(m => m != null);
        }

        //Once folder is found, check its children for an item named "Header".
        var header = siteComp.Children()
            .Where(m => m.Published && m.ContentType.Alias == "ciHeader")
            .Select(m => _glassService.CreateType<Header>(m))
            .FirstOrDefault();
        return PartialView("~/Views/Partials/_Header.cshtml", header);
    }
    catch (Exception e)
    {

    }
    return PartialView("~/Views/Partials/_Header.cshtml");
}

(Views: 26)

Leave a Reply

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

Time limit is exhausted. Please reload the CAPTCHA.