Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Roman 9 posts 79 karma points
    Mar 19, 2015 @ 19:36
    Roman
    0

    How to know is current document list-view-enabled?

    Hi.

    How to know from Razor template if some document {Umbraco.Web.Models.DynamicPublishedContent} is list-view-enabled? Because if it is, I don't want to render its children in website's navigation tree.

    Thanks

  • Roman 9 posts 79 karma points
    Mar 20, 2015 @ 17:50
    Roman
    100

    Ok, I got that myself.

    I deleted all DLLs out of bin folder, downloaded Umbraco 7.2.2 sources, added them to my VS solution to understand how everything works. Turns out there is Umbraco.Web\Trees\ContentTreeController.cs:

    protected override TreeNode GetSingleTreeNode(IUmbracoEntity e, string parentId, FormDataCollection queryStrings)
    {
        var entity = (UmbracoEntity) e;
    
        var allowedUserOptions = GetAllowedUserMenuItemsForNode(e);
        if (CanUserAccessNode(e, allowedUserOptions))
        {
    
            //Special check to see if it ia a container, if so then we'll hide children.
            var isContainer = e.IsContainer();   // && (queryStrings.Get("isDialog") != "true");
    
            var node = CreateTreeNode(
                e.Id.ToInvariantString(),
                parentId,
                queryStrings,
                e.Name,
                entity.ContentTypeIcon,
                entity.HasChildren && (isContainer == false));
    

    and

    Umbraco.Core\Models\UmbracoEntityExtensions.cs:

    internal static class UmbracoEntityExtensions
    {
    
        // ...
    
        public static bool IsContainer(this IUmbracoEntity entity)
        {
            if (entity.AdditionalData.ContainsKeyIgnoreCase("IsContainer") == false) return false;
            var val = entity.AdditionalData.GetValueIgnoreCase("IsContainer", null);
            if (val is bool && (bool) val)
            {
                return true;
            }
            return false;
        }
    
    }
    

    so I did the same thing, TLayout.cshtml:

                        var menuItems = homePage.Children;
    
                        // ...
    
                        IUmbracoEntity entity = ApplicationContext.Services.EntityService.Get(item.Id, UmbracoObjectTypes.Document);
    
                        object val = null;
                        entity.AdditionalData.TryGetValue("IsContainer", out val);
    
                        bool isContainer = val is bool && (bool)val;
    
    
                        if (item.Children.Count() > 0 && !isContainer)
                        {
                            // Display item and every child
                        }
                        else
                        {
                            // Display item only, just like in Content panel of Umbraco backoffice
                        }
    

    Profit!

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 27, 2017 @ 07:05
    Dave Woestenborghs
    1

    Hi Roman,

    You are using the ApplicationContext.Services.EntityService in your view. I would strongly advice you not to do this.

    The services in ApplicationContext.Services are ment for doing CRUD operations. This means that they will the database everytime you run this code and bypass the Umbraco published content cache.

    If your site will get a lot of traffic this will become a performance bottleneck. Read the part in this document describing common pitfalls : https://our.umbraco.org/documentation/Reference/Common-Pitfalls/#using-the-services-layer-in-your-views

    I would solve it differently. You know which document type you enabled the list view on. So you can also set a property on that doctype (eg a read only label) to indicate it has a list view enabled. Than you can check if that property exists on the current document

    @Model.HasProperty("yourPropertyAlias")

    Another way is just handling it is by checking the doctype in code.

    Dave

Please Sign in or register to post replies

Write your reply to:

Draft