I am trying to build a navigation that automatically includes certain pages depending on settings in the CMS.
I have used the sitemap Partial Macro and have tried to add an if statement that checks a page property like so: @if (!item.HideSubPages) {
However, I am just getting the following error:
Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'HideSubPages' and no extension method 'HideSubPages' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
Here is my full code
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Umbraco.Core.Models
@using Umbraco.Web
@*
This snippet makes a list of links of all visible pages of the site, as nested unordered HTML lists.
How it works:
- It uses a custom Razor helper called Traverse() to select and display the markup and links.
*@
@{ var selection = Model.Content.Site(); }
<div class="sitemap">
@* Render the sitemap by passing the root node to the traverse helper, below *@
@Traverse(selection)
</div>
@* Helper method to traverse through all descendants *@
@helper Traverse(IPublishedContent node)
{
@* Update the level to reflect how deep you want the sitemap to go *@
const int maxLevelForSitemap = 4;
@* Select visible children *@
var selection = node.Children.Where(x => x.IsVisible() && x.Level <= maxLevelForSitemap).ToArray();
@* If any items are returned, render a list *@
if (selection.Length > 0)
{
<ul>
@foreach (var item in selection)
{
<li class="level-@item.Level">
<a href="@item.Url">@item.Name</a>
@* Run the traverse helper again for any child pages *@
@if (!item.HideSubPages) {
@Traverse(item)
}
</li>
}
</ul>
}
}
Try
item.GetPropertyValue<bool>("hideSubPages")
instead?
You are expecting a dynamic type but are dealing with IPublishedContent. Dynamics are not future proof to use anyway as support for them will end in the next "big" version of Umbraco (v8), I believe. So GetProperty/GetPropertyValue is your friend :-)