A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.umbraco.com/umbraco-cms/reference/querying/ipublishedcontent/collections below:

IPublishedContent Collections | Umbraco CMS

IPublishedContent Collections | Umbraco CMS
  1. Reference
  2. Querying & Models
  3. IPublishedContent
IPublishedContent Collections

All collections of IPublishedContent are IEnumerable<IPublishedContent>. This means that all C# LINQ statements can be used to filter and query the collections.

Returns a collection of child items available in the current culture, below the current content item.

<ul>
    @foreach(var item in Model.Children)
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>

Returns a collection of child items for all cultures, below the current content item, regardless of whether they are available for the current culture.

<ul>
    @foreach(var item in Model.ChildrenForAllCultures)
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>
.Children(string culture = null)

Returns a collection of child items available in the specified culture with a default of the current one, below the current content item.

<ul>
    @foreach(var item in Model.Children("dk-dk"))
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>

Returns all ancestors of the current page (parent page, grandparent and so on)

<ul>
    @*Order items by their Level*@
    @foreach(var item in Model.Ancestors().OrderBy(x => x.Level))
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>

Returns the first ancestor of the current page

@* return the first ancestor item from the current page *@
var nodes = Model.Ancestor();

@* return the first item, of a specific type, from the current page *@
var nodes = Model.Ancestor<DocumentTypeAlias>();

Returns a collection of all ancestors of the current page (parent page, grandparent and so on), and the current page itself

@* Get the top item in the content tree, this will always be the Last ancestor found *@
var websiteRoot = Model.AncestorsOrSelf().Last();

Returns all descendants of the current page (children, grandchildren etc)

<ul>
    @* Filter collection by content that has a template assigned *@
    @foreach(var item in Model.Descendants().Where(x => x.TemplateId > 0))
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>

Returns all descendants of the current page (children, grandchildren etc), and the current page itself

<ul>
    @* Filter collection by content that has a template assigned *@
    @foreach(var item in Model.DescendantsOrSelf().Where(x => x.TemplateId > 0))
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>

Filters a collection of content by content type alias

<ul>
    @* Filter collection by content type alias (you can pass in any number of aliases) *@
    @foreach(var item in Model.DescendantsOrSelf().OfTypes("widget1", "widget2"))
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
</ul>
Filtering, Ordering & Extensions

Filtering and Ordering are done with LINQ.

Some examples:

@* Returns all items in the collection that have a template assigned and have a name starting with 'S' *@
var nodes = Model.Descendants().Where(x => x.TemplateId > 0 && x.Name.StartsWith("S"))
@* Orders a collection by the property name "title" *@
var nodes = Model.Children.OrderBy(x => x.GetProperty("title"))

Groups collection by content type alias

@{
    var groupedItems = Model.Descendants().GroupBy(x => x.ContentType);
    foreach (var group in groupedItems)
    {
        <h2>@group.Key.Alias</h2>
        foreach(var item in group)
        {
            <h3>@item.Name</h3>
        }
    }
}

Return only the number of items for a collection specified by the integer value.

@* return the first 3 items from the child collection *@
var nodes = Model.Children.Take(3);

Return items from the collection after skipping the specified number of items.

@* Skip the first 3 items in the collection and return the rest *@
var nodes = Model.Children.Skip(3);

You can combine Skip and Take when using for paging operations

@* using skip and take together you can perform paging operations *@
var nodes = Model.Children.Skip(10).Take(10);

Returns the number of items in the collection

int numberOfChildren =  Model.Children.Count();

Returns a boolean True/False value determined by whether there are any items in the collection

bool hasChildren =  Model.Children.Any();

Some filtering and routing behaviour is dependent upon a set of special naming conventions for certain properties. See also: Routing Property Conventions

If you create a checkbox property on a document type with an alias umbracoNaviHide then the value of this property is used by the IsVisible() extension method when filtering.

IEnumerable<IPublishedContent> sectionPages =  Model.Children.Where(x => x.IsVisible());

Use case: When displaying a navigation menu for a section of the site, following this convention gives editors the option to 'hide' certain pages from appearing in the section navigation. (hence the unusual umbracoNaviHide property alias!)

Last updated 4 months ago


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4