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>
}
}
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
@{
var adminTopNavigation = Model.Value<IEnumerable<Link>>("topNavigation");
}
@foreach (var link in adminTopNavigation)
{
<a href="@link.Url">@link.Name</a>
}
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.
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.
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
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:
But if I try to get the navigation from the Admin Settings Page using similar code:
I get nothing...
Is your code entering the if statement or is adminTopNavigation null?
Where exactly are you using this code?
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]
This is the code on top navigation
Is adminSettingsPage getting a value? If not then it probably isn't an ancester of the Umbraco.AssignedContentItem
adminSettingsPage is on the root level of the site.. .
What should I use to get the values if not "AssignedContentItem.AncestorOrSelf"
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
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
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
I also tested using
But then how do I assigned the page to get the content from? I do not see TopNavigation as an option
This will return an IPublishedContent so you will then need to use something like
to get your navigayion object
I see
works well and I am able to do
to display the page name.
when I run
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...
try something like this
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:
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
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.
Hi,
At the top of your partial view add this
Then inside your
<ul></ul>
tags add thisThank you!!!
that seems to work
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
Hi Howardd,
That is not what they were asking for.
I am struggling with the same problem as you. Lucky to read this article, Thank you very much for everyone's help!
is working on a reply...