Copied to clipboard

Flag this post as spam?

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


  • Yaco Zaragoza 80 posts 334 karma points
    Mar 31, 2023 @ 14:05
    Yaco Zaragoza
    0

    Reading property from a Settings Page (I am using Umbraco 11.1.0)

    I created 2 page types the 1st one called "Home Page" (homePage) and the 2nd called "Admin Settings Page" (adminSettingsPage)

    Both Pages inherit from the same composition "Top Navigation Properties" which has a Multi-URL Picker called "topNavigation"

    I am able to render the navigation that was created using the Home Page with the following code:

    // Get navigation from Home Page
    var homePage = Umbraco.AssignedContentItem.AncestorOrSelf<HomePage>();
    var children = homePage?.TopNavigation;
            @if (children != null && children.Any())
            {
                @foreach (var navLink in children)
                {
                    <li class="nav-item">
                        <a href="@navLink.Url">@navLink.Name</a>
                    </li>
                }
            }
    

    But if I try to get the navigation from the Admin Settings Page using similar code:

    // Get Navigation based on Admin Settings Page
    var adminSettingsPage = Umbraco.AssignedContentItem.AncestorOrSelf<AdminSettingsPage>();
    var adminTopNavigation = adminSettingsPage.TopNavigation;
    
            @if (adminTopNavigation != null && adminTopNavigation.Any())
            {
                @foreach (var adminNavLink in adminTopNavigation)
                {
                    <li class="nav-item">
                        <a href="@adminNavLink.Url">@adminNavLink.Name</a>
                    </li>
                }
            }
    

    I get nothing...

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Mar 31, 2023 @ 15:23
    Huw Reddick
    0

    Is your code entering the if statement or is adminTopNavigation null?

    Where exactly are you using this code?

  • Yaco Zaragoza 80 posts 334 karma points
    Mar 31, 2023 @ 15:31
    Yaco Zaragoza
    0

    I am trying to add it to a partial called "topnavigation.cshtml" called from my master template

    This is the code on my master template file (master.cshtml) [I removed some of the HTML for simplicity]

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @{
        Layout = null;
    }
    
    @await Html.PartialAsync("~/Views/Partials/_top_navigation.cshtml")
    

    This is the code on top navigation

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    
    @{
        // Get navigation from Home Page
        var homePage = Umbraco.AssignedContentItem.AncestorOrSelf<HomePage>();
        var topNavigation = homePage?.TopNavigation;
        //var leftNavigation = homePage?.LeftNavigation;
    
        // Get Navigation based on Admin Settings 
        var adminSettingsPage = Umbraco.AssignedContentItem.AncestorOrSelf<AdminSettingsPage>();
        var adminTopNavigation = adminSettingsPage?.TopNavigation;
    }
    
    
    
    
                @if (adminTopNavigation != null && adminTopNavigation.Any())
                {
                    @foreach (var adminNavLink in adminTopNavigation)
                    {
                        <li class="nav-item">
                             <a class="nav-link text-light fw-bold" href="@adminNavLink.Url">@adminNavLink.Name</a>
                         </li>
                    }
                }
    
                    @if (topNavigation != null && topNavigation.Any())
                {
    
                        @foreach (var navLink in topNavigation)
                    {
    
                        <li class="nav-item">
                            <a class="nav-link text-light fw-bold" href="@navLink.Url" target="@navLink.Target">@navLink.Name</a>
                        </li>
    
                    }
                }
    
  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Mar 31, 2023 @ 16:04
    Huw Reddick
    0

    Is adminSettingsPage getting a value? If not then it probably isn't an ancester of the Umbraco.AssignedContentItem

  • Yaco Zaragoza 80 posts 334 karma points
    Mar 31, 2023 @ 16:08
    Yaco Zaragoza
    0

    adminSettingsPage is on the root level of the site.. .

    What should I use to get the values if not "AssignedContentItem.AncestorOrSelf"

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Mar 31, 2023 @ 16:14
    Huw Reddick
    0

    just because it is at the root does not necessarily make it an ancestor :) that depends where your current page sits in relation to adminSettings

    You could try using ContentAtRoot

  • Yaco Zaragoza 80 posts 334 karma points
    Mar 31, 2023 @ 16:16
    Yaco Zaragoza
    0

    What I am trying to do is create a admin Settings Page from where I can create the top and left navigations.

    If I try

    var adminSettingsPage = Umbraco.AssignedContentItem.ContentAtRoot<AdminSettingsPage>();
    

    I see An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately. C:\Users\yzaragoza\source\repos\BSOInformantV3\Views\Partials_top_navigation.cshtml

    'IPublishedContent' does not contain a definition for 'ContentAtRoot' and no accessible extension method 'ContentAtRoot' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?) + var adminSettingsPage = Umbraco.AssignedContentItem.ContentAtRoot

  • Yaco Zaragoza 80 posts 334 karma points
    Mar 31, 2023 @ 16:27
    Yaco Zaragoza
    0

    I also tested using

     var adminSettingsPage1 = Umbraco.ContentAtRoot;
    

    But then how do I assigned the page to get the content from? I do not see TopNavigation as an option

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Mar 31, 2023 @ 16:35
    Huw Reddick
    0
    var adminSettingsPage = Umbraco.ContentAtRoot().First(x=>x.ContentType.Alias == "adminSettingsPage")
    

    This will return an IPublishedContent so you will then need to use something like

    adminSettingsPage.Value<TopNavigation>("topNavigation")
    

    to get your navigayion object

  • Yaco Zaragoza 80 posts 334 karma points
    Mar 31, 2023 @ 17:57
    Yaco Zaragoza
    0

    I see

    var adminSettingsPage = Umbraco.ContentAtRoot().First(x => x.ContentType.Alias == "adminSettingsPage");
    

    works well and I am able to do

    @adminSettingsPage.Name
    

    to display the page name.

    when I run

    var adminTopNavigation = adminSettingsPage.Value<TopNavigation>("topNavigation");
    

    What should the namespace be?

    I see inside ("topNavigation") is the name of the property I want to get in this case a multi URL Picker...

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Apr 01, 2023 @ 12:11
    Huw Reddick
    0

    try something like this

    @{
        var adminTopNavigation = Model.Value<IEnumerable<Link>>("topNavigation");
    }
    
    @foreach (var link in adminTopNavigation)
    {
        <a href="@link.Url">@link.Name</a>
    }
    
  • Yaco Zaragoza 80 posts 334 karma points
    Apr 03, 2023 @ 14:22
    Yaco Zaragoza
    0

    Overall, what I am trying to do is create a Admin Settings Page that will allow me to create Global Properties like topNavigation (Multi URL Picker) ,leftNavigation (Multi URL Picker), and footerNavigation (Multi URL Picker), etc. and call them from the master template.

    The code still asked for a namespace inside of the <>

    so I tried:

    var adminTopNavigation = Model.Value<System.Collections.Generic.IEnumerable<Umbraco.Cms.Core.Models.Link>>("topNavigation");
    

    When I tested that code and I am getting the following error:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    I am attaching screen shots from my local enviorment so you can hopefully better understand what structure and identify what I am missing.

    My Master templae still has the following code

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @{
        Layout = null;
    }
    
    @await Html.PartialAsync("~/Views/Partials/_top_navigation.cshtml")
    

    Thank you so much for your help!

    P.S I removed the top_navigation composition and added the properties directly into the Admin Settings Page to hopefully make the solution to this simpler.

    Please let me know your opinion on what you would recommend as best practice.

    Top Navigation Partial

    Content Home Page

    Content Settings Page

    Admin Settings Page

    Home Setting Page

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Apr 03, 2023 @ 15:33
    Huw Reddick
    0

    Hi,

    At the top of your partial view add this

    @using Umbraco.Cms.Core.Models
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @{
    
        var adminSettingsPage = Umbraco.ContentAtRoot().First(x => x.ContentType.Alias == "adminSettingsPage");
        var adminTopNavigation = adminSettingsPage.Value<IEnumerable<Link>>("topNav");
    }
    

    Then inside your <ul></ul> tags add this

    @foreach (var link in adminTopNavigation)
    {
        <li><a href="@link.Url">@link.Name</a></li>
    }
    
  • Yaco Zaragoza 80 posts 334 karma points
    Apr 03, 2023 @ 15:41
    Yaco Zaragoza
    0

    Thank you!!!

    that seems to work

  • Howardd 1 post 71 karma points
    Apr 04, 2023 @ 09:11
    Howardd
    0

    To read a property from a settings page in Umbraco 11.1.0, you can use the Umbraco.Core.Configuration.SettingsManager class to get the settings object and then access the property using its name. For example: css Copy code var settings = SettingsManager.Instance.AppSettings; var myProperty = settings["MyPropertyName"]; Replace "MyPropertyName" with the name of the property you want to read. Mybkexperience

  • Huw Reddick 1737 posts 6077 karma points MVP c-trib
    Apr 04, 2023 @ 10:41
    Huw Reddick
    0

    Hi Howardd,

    That is not what they were asking for.

  • alishakihn1 2 posts 71 karma points
    Apr 19, 2023 @ 04:22
    alishakihn1
    0

    I am struggling with the same problem as you. Lucky to read this article, Thank you very much for everyone's help!

Please Sign in or register to post replies

Write your reply to:

Draft