Copied to clipboard

Flag this post as spam?

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


  • Nadine Fisch 159 posts 429 karma points
    Sep 15, 2017 @ 07:30
    Nadine Fisch
    0

    read properties from a settingspage in my partialHTML-View

    Hey guys,

    I've created a "general Settings" page below the root Element. In my partialHTML-Views I want to read those properties, but I have problems with getting those values.

    First, I tried to select the properties-page with this segment:

       var rootNodes = Umbraco.TypedContentAtRoot();
       var settings = rootNodes.First(x => x.DocumentTypeAlias == "GeneralSettings");
    

    I only have access to the GetPropertyValues()-Method for the settings-page, if i put this code outside of my RenderPartial and transfer it as model-object to a partialHTML. I tried to put this variable also in a ViewBag. But it is still not working.

    I would like to use this settings-variable globally, but I have no idea how to achieve this. Because the current project environment doesn't allow me to create Controller-Classes. I may only write code in the frontend-templates. So I am limited in my actions.

    Do you have any ideas?

    Thanks in advance.

  • Lewis Smith 211 posts 620 karma points c-trib
    Sep 15, 2017 @ 08:37
    Lewis Smith
    0

    Hi Nadine,

    I do the same when creating websites through Umbraco.

    My technique is the following:

    var homePage = Model.Site();
    var Settings = homePage.Descendants("generalSettings").FirstOrDefault();
    if(Settings == null)
    {
         //Return null or fix as settings returning full with make the website break
         return;
    }
    

    I work using IPublishedContent:

    @inherits UmbracoViewPage<IPublishedContent>
    

    at the top of my partial so this may differ slightly for you depending on what you are using.

    Thanks, Lewis

    (side note, when trying to get content from the settings tab you can do this easily by the following:)

    String ContactNumber = Settings.GetPropertyValue<string>("contactNumber");
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 15, 2017 @ 09:39
    Alex Skrypnyk
    0

    Hi Nadine,

    Is it possible to you to move settings properties to root node?

    It will be the most efficient way in a sense of performance of page loading because code like getting settings node that is the same level as root but not home page - it's not needed traversing over the tree. We can avoid this traversing if we move properties to root node.

    Lewis, this code isn't fast:

    var homePage = Model.Site();
    var Settings = homePage.Descendants("generalSettings").FirstOrDefault();
    if(Settings == null)
    {
         //Return null or fix as settings returning full with make the website break
         return;
    }
    

    Add this code to each page where you need settings - potential performance problem.

    Thanks,

    Alex

  • Nadine Fisch 159 posts 429 karma points
    Sep 15, 2017 @ 09:58
    Nadine Fisch
    0

    my generalSettings - Page is on the root node. I'll give it a try with searching for DocumentType instead, maybe the first()-Method returns the wrong object.

    I inherit from Umbraco.Web.Mvc.UmbracoTemplatePage, maybe this is the problem?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 15, 2017 @ 12:40
    Alex Skrypnyk
    0

    Umbraco.Web.Mvc.UmbracoTemplatePage shouldn't be a problem.

    "GeneralSettings" is it a right doctype alias?

  • Nadine Fisch 159 posts 429 karma points
    Sep 15, 2017 @ 20:10
    Nadine Fisch
    0

    Yeah, I used the correct Alias. But if I use the same code in a partialHTML the object is not a IPublishedContent. It was rather a XML-Object, I don't remember at the moment.

    So I thought it has something to do with the namespace.

  • Marcio Goularte 388 posts 1360 karma points
    Sep 15, 2017 @ 20:20
    Marcio Goularte
    1

    Hello, when I have a node that is settings:

    var settings = Umbraco.TypedContentSingleAtXPath("//settings");
    
Please Sign in or register to post replies

Write your reply to:

Draft