Hi - I am new to Umbraco. I am trying to sort pages in a nav macro based on a custom numeric property in their doc type. I don't get an error, but the pages aren't sorted. Here is the entire macro:
<nav>
<ul id="topNavigation">
@{ var homeNode = Model.AncestorOrSelf("HomePage"); }
<li><a href="@homeNode.Url" class="@Library.If(homeNode.Id == Model.Id, "current", "")">@homeNode.pageTitle</a></li>
@foreach (var page in homeNode.Children.Where("Visible").OrderBy("sortOrder"))
{
if (page.HasProperty("pageTitle"))
{
<li>
<a href="@page.Url" class="@Library.If(page.Id == Model.Id, "current", "")">@page.pageTitle</a>
<!-- If the page has child nodes (2nd level) that are visible -->
@if (page.Children.Where("Visible").Count() > 0)
{
<ul class="childNavList">
@foreach (var childPage in page.Children.Where("Visible").OrderBy("sortOrder"))
{
<li><a href="@childPage.Url" class="@Library.If(childPage.Id == Model.Id, "current", "")">@childPage.pageTitle</a></li>
}
</ul>
}
else if (page.Name == "Map Page") {
<ul class="childNavList">
<li><a href="/map-page.aspx?q=driver">Driver</a></li>
<li><a href="/map-page.aspx?q=staff">Staff</a></li>
<li><a href="/map-page.aspx?q=warehouse">Warehouse</a></li>
</ul>
}
</li>
}
}
</ul>
</nav>
It looks like examples I have found for using the OrderBy method, so I'm not sure what the problem is. Any help you can offer is appreciated.
Thanks for the response Jan, but all I figured out is that it is actually sorting by the when the page was created (or Id) rather than my custom sortOrder property. I thought maybe "sortOrder" was already a property in Umbraco, so I tried changing it to navViewOrder, but I am still experiencing the same problem.
Edit: Actually, renaming the property did fix it - there was just a database problem the first time I tried it.
Coming to think of it...why do you sort the documents using af custom property?
When you fetch the nodes it displays in the sort order that it has in the backoffice. So if you change the sort order by right clicking a parent node and then drag and drop nodes around so it has a new sort order this will be displayed on the website. Having a custom sort order property seems double to me :)
When we tried dragging the nodes around, it did not update their order in our nav. It looks like Umbraco sorts by Id by default, rather than sortOrder. I did not know of the existence of sortOrder until I ran into this problem by also naming my custom property sortOrder (after my last post I found that there is, in fact, a default property saved in the nodes called sortOrder in the umbraco.config file). So, yes, you are correct - having another property for sort recreates existing functionality, but we also found it very difficult to drag and drop the nodes, so being able to just fill in the sort order manually will help us in that regard.
Edit: To clarify, we were dragging and dropping the actual nodes - I was not aware of the sort menu. Thank you for informing me of that.
After you did the sorting did you then right click the "Content" node (the default node) and choose "Republish entire site"? This should make the changes appear on the website.
It looks like dragging and dropping the actual nodes does not change the sort - only using the sort menu will save the sort of the nodes. The sort menu is much easier to use than moving the nodes, so it works out great. It looks like I don't need my custom property. Thanks, you have been very helpful.
OrderBy on numeric property not working
Hi - I am new to Umbraco. I am trying to sort pages in a nav macro based on a custom numeric property in their doc type. I don't get an error, but the pages aren't sorted. Here is the entire macro:
It looks like examples I have found for using the OrderBy method, so I'm not sure what the problem is. Any help you can offer is appreciated.
I found that if I sort by
it will sort by the id / created date descending rather than my sortOrder property. Is this a bug?
Hi rfiegle - welcome to our :)
Cool that you managed to figure out the issue by yourself.
In short, no it's not a bug - unless you specifically add "desc" for descending the default sortorder will be "asc" for ascending.
You can read a bit more about OrderBy here http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4.aspx and then I'll recommend that you download the Razor cheat sheet.
I hope you have a fun time learning Umbraco - please don't hesitate to ask any kind of questions if there is something you're in doubt about.
/Jan
Thanks for the response Jan, but all I figured out is that it is actually sorting by the when the page was created (or Id) rather than my custom sortOrder property. I thought maybe "sortOrder" was already a property in Umbraco, so I tried changing it to navViewOrder, but I am still experiencing the same problem.
Edit: Actually, renaming the property did fix it - there was just a database problem the first time I tried it.
Coming to think of it...why do you sort the documents using af custom property?
When you fetch the nodes it displays in the sort order that it has in the backoffice. So if you change the sort order by right clicking a parent node and then drag and drop nodes around so it has a new sort order this will be displayed on the website. Having a custom sort order property seems double to me :)
/Jan
When we tried dragging the nodes around, it did not update their order in our nav. It looks like Umbraco sorts by Id by default, rather than sortOrder. I did not know of the existence of sortOrder until I ran into this problem by also naming my custom property sortOrder (after my last post I found that there is, in fact, a default property saved in the nodes called sortOrder in the umbraco.config file). So, yes, you are correct - having another property for sort recreates existing functionality, but we also found it very difficult to drag and drop the nodes, so being able to just fill in the sort order manually will help us in that regard.
Edit: To clarify, we were dragging and dropping the actual nodes - I was not aware of the sort menu. Thank you for informing me of that.
After you did the sorting did you then right click the "Content" node (the default node) and choose "Republish entire site"? This should make the changes appear on the website.
Hope this helps.
/Jan
It looks like dragging and dropping the actual nodes does not change the sort - only using the sort menu will save the sort of the nodes. The sort menu is much easier to use than moving the nodes, so it works out great. It looks like I don't need my custom property. Thanks, you have been very helpful.
is working on a reply...