Building a Queries in Umbraco 12+

40 Views Asked by At

I am trying to create a Dynamic Left Navigation using Umbraco 12.

I created a page type called Content Page (alias contentPage) that has 2 compositions Content and Page Settings

In Page Settings, I added 2 fields the 1st one is called "Show on Left Nav" (which is a true or false) this field will be to determine if the page will show as a link on the left navigation.

The 2nd one is called Left Nav Section (alias leftNavigationSection) (Which is a dropdown with the values for the different sections that will have different navigations) - This one will determine what section the page belongs to

Here is an example of what the data will look like

  • Home
  • Human Resources (Show in Left Nav Y and Section Human Resources)
    • Employee Forms (Show in Left Nav Y and Section Human Resources)
    • Budget (Show in Left Nav N and Section Human Resources)
    • Employee Info (Show in Left Nav N and Section Human Resources)
    • Documents (Show in Left Nav N and Section Human Resources)

when you visit any of the pages under the Human Resources section (Employee Forms, Documents, etc. ) The query should return the links

  • Human Resources
    • Employee Forms

because they belong to the "Human Resources" section and the left nav setting is yes

While the links for

  • Budget
  • Employee Info
  • Documents

should be hidden from the left nav because the left nav setting is No

No matter what page in the "Human Resources" section you are viewing you should load all the visible links for the section the page belongs to

This is the query I have so far

 var selectLeftNav = Model.Ancestor()
.Children<ContentPage>()
.Where(x => x.IsVisible())
.Where(x => x.ShowOnLeftNavigation == true)
.Where(x => x.LeftNavigationSection == "Human Resources");

<ul class="nav left-nav-items">
    @foreach (var item in selectLeftNav)
    {
     <li class="nav-item"><a href="@item.Url()">@item.Name</a></li>
    }
</ul>

The value for Model.Value("leftNavigationSection") comes from a Dropdown and I can print it to the screen without any issues

In conclusion, I am trying to read the setting for the page the user is on and based on that value show the left navigation items that are the same ..

Over all I am asking, How can I build my own query based on a parameter I am adding all my to all my content types. (without using the query builder that generates a GUID)

2

There are 2 best solutions below

1
Jannik Anker On

Typically, when using the "Ask a question" feature on StackOverflow (or anywhere else for that matter), at least part of what you write should be in the form of an actual question. But let's just assume that the code you have included isn't working as intended?

For anyone to be able to help, you should include as many details as possible as to:

  • how does the code not work? Does it throw an error? Does it just not render anything? Is it showing the wrong things?
  • what have you tried in order to make it work, before you ended up here?
  • what is x.LeftNavigationSection? It could be a boolean, a string, or a content picker for all we know. Nothing you wrote helps us understand the thoughts behind the content models or your intentions behind them.
  • What's in the dropdown you mention? Have you tried rendering that value alone to see if it even makes sense to compare it to x.LeftNavigationSection?
  • It seems a bit strange that you are using Model.AncestorOrSelf to find your nav, but as I have zero context of what is selected where and how things are connected, I couldn't possibly tell you if it's right or wrong.

TL;DR: could you please update the "question" with more details?

2
Jannik Anker On

You still haven't described what the actual problem is - are you getting errors or "just" no content from the query?

But okay! Given a bit more information I think I can spot a few things to look at :-)

  1. Model.AncestorOrSelf() isn't really useful. It will give you either the current node or its nearest ancestor, according to the docs. The same docs say that the method doesn't really make sense as it will almost always just return the current content :-/ Maybe try using Model.Ancestor() or Model.Parent instead?
  2. As you are selecting children of a specific type (ContentPage) I'm assuming you are using ModelsBuilder generated content models. Does your view also use said models to "cast" the Model? As in, something like: @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentPage> ? In that case you should be able to use Model.LeftNavigationSection directly, instead of using Model.Value(...). That will give you a typed property value of some sort - I still can't really figure out if those two values are actually comparable, though. Worth taking a look at.
  3. Using x.IsVisible() is fine, but you also need to check for x.ShowInLeftNav or whatever your boolean property is called :-)

Hope this helps you get closer!