Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 103 posts 295 karma points
    Sep 02, 2020 @ 09:46
    Martin Rud
    0

    Best way to make settings node values available in views

    Hi,

    I have the below structure where I have two content nodes containing different data to be used across a lot of view files.

    enter image description here

    Right now I have added this code in every view file where I need to access these data.

    var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault().DescendantsOrSelf().OfTypes("site_folder_settings").FirstOrDefault();
    var settingsAdmin = settingsFolder.DescendantsOrSelf().OfTypes("site_settings_admin").FirstOrDefault();
    var settingsEditor = settingsFolder.DescendantsOrSelf().OfTypes("site_settings_editor").FirstOrDefault();
    

    And then I fetch values this way:

    <div>@settingsEditor.Value("footerCol1")</div>
    

    That is both redundant and probably also waste of ressources to do the same queries multiple times.

    I am looking for a way to do the queries once in an *.cs file in App_Code where I then can call them from view files. But my skills are not enough to that now. Can anyone give me a hint?

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Sep 02, 2020 @ 22:35
    Alex Skrypnyk
    0

    Hi Martin,

    There are few ways to optimize queries like that.

    1) You can just cache the view where this code is placed

    2) As I see you don't need descendants method, which one is little bit heavy, try to use Children instead:

    var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault()
                .Children.FirstOrDefault(x => x.ContentType.Alias.Equals("global_doc"))
                .Children.FirstOrDefault(x => x.ContentType.Alias.Equals("site_folder_settings"));
    
            var settingsAdmin = settingsFolder.Children.OfTypes("site_settings_admin").FirstOrDefault();
            var settingsEditor = settingsFolder.Children.OfTypes("site_settings_editor").FirstOrDefault();
    

    Please replace "global_doc" with Global folder doctype alias.

    Thanks,

    Alex

  • Martin Rud 103 posts 295 karma points
    Sep 03, 2020 @ 05:36
    Martin Rud
    0

    Hi Alex,

    Thanks for your comments.

    Regarding bullet 2: If I change this:

    var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault().
        DescendantsOrSelf().OfTypes("site_folder_settings").FirstOrDefault();
    

    to your suggestion:

    var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault()
        .Children.FirstOrDefault(x => x.ContentType.Alias.Equals("site_folder_settings"));
    

    Then settingsFolder suddenly becomes null (which is also a surprise to me).


    Regarding bullet 1: Caching sounds like a good idea. But what about all the other content of the view that I cache? Won´t everything be cached (which I do not want)?

    In FB-forum "Umbraco Web Developers" (https://www.facebook.com/groups/202933450108554) I have been pointed to a similar solution that is the one I will now test: https://www.dot-see.com/en/blog/caching-umbraco-nodes-for-use-in-multiple-places/

  • Alex Skrypnyk 5908 posts 22603 karma points MVP 4x admin c-trib
    Sep 03, 2020 @ 12:36
    Alex Skrypnyk
    0

    In this code snippen you miss "Global" node I think

    var settingsFolder = Umbraco.ContentAtRoot().FirstOrDefault()
        .Children.FirstOrDefault(x => x.ContentType.Alias.Equals("site_folder_settings"));
    
  • Martin Rud 103 posts 295 karma points
    Sep 03, 2020 @ 18:29
    Martin Rud
    0

    I don´t think so. It has the same "strucure" as the working code (only ".DescendantsOrSelf()" has been replaced with ".Children").

  • Sotiris Filippidis 274 posts 1395 karma points
    Sep 03, 2020 @ 18:28
    Sotiris Filippidis
    100

    Following my reply on the FB Umbraco Group on the same question, I'm posting this also here hoping that it can be helpful for anyone investigating approaches to caching "special" nodes:

    I use a "cached nodes" approach where I cache some nodes (usually unique ones, like settings etc) in the runtime cache (clearing it whenever something is published) and then using this wherever it's needed. Goes hand in hand with modelsbuilder.

    Blog post (and embedded gist) here: https://www.dot-see.com/en/blog/caching-umbraco-nodes-for-use-in-multiple-places/

  • Martin Rud 103 posts 295 karma points
    Sep 04, 2020 @ 06:52
    Martin Rud
    0

    Thanks, Sotiris. I mark your answer as a solution. I haven´t tested it yet, but it seems like the approach I looked for.

    For now my .NET and Umbraco skills are not sufficient to implement your solution, but I will get there. :)

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Sep 04, 2020 @ 07:38
    Tim Geyssens
    0

    is that v8 Martin? What I did is create an umbraco helper extension that you can use for easily accessing these setting nodes without having the login in your views...

    you can then do stuff like @Umbraco.WebSiteSettings.Footer

    We have been discussing Sotiris work on the slack channel and it feels a bit strange to cache cached content, will probably result in unwanted effects..

  • Sotiris Filippidis 274 posts 1395 karma points
    Sep 04, 2020 @ 12:17
    Sotiris Filippidis
    0

    I've updated the post with all the useful parts from our discussion in Slack, providing possible answers and alternative ways to implement this. Hope it'll be good as a starting point for anyone who reads it and needs to decide how to implement something similar.

  • Martin Rud 103 posts 295 karma points
    Sep 04, 2020 @ 15:58
    Martin Rud
    0

    Cool! 💪

  • Martin Rud 103 posts 295 karma points
    Sep 04, 2020 @ 07:42
    Martin Rud
    0

    Thanks for the info.

    Yes, it´s v8. Have you got an example or some kind of directions you can point me?

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Sep 04, 2020 @ 08:17
  • Martin Rud 103 posts 295 karma points
    Sep 04, 2020 @ 16:02
    Martin Rud
    0

    Thanks. I will maybe try this at a later point. For now I can see that I lack some bit of knowledge into .NET i general and Umbraco Models Builder in particular.

    So for now I will put my site settings on the home node an use fallback: Fallback.ToAncestors

    This is ok for me since for the particular site I am building editors are allowed to see and edit them.

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Sep 04, 2020 @ 08:05
    Tim Geyssens
    0

    Yes, just writing a post hold on :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies