Sam
Sam

Reputation: 10113

Umbraco Querying from Macro Script?

Umbraco Version = 6.0.3

I'm trying to do some seemingly simple stuff in a macro scriptlet. Basically, I want to loop through all of the visible child content that is not a category:

@inherits umbraco.MacroEngines.DynamicNodeContext

@{
var subs = Model.Children.Where("Visible && DocumentTypeAlias != \"Category\"");
}
<span>Count: @subs.Count()</span>
@if (subs.Any())
{
    <ul>            
        @foreach (var sub in subs)
        {
            <li>
                <a href="@sub.Url">@sub.Name</a>
            </li>
        }
    </ul>
}

If I take out the "Visible" portion of the where clause, it works correctly (with the exception of displaying content marked as hidden). I can also use "Visible" on it's own by removing the "DocumentTypeAlias", but then all visible content including categories are displayed.

I also tried using strongly typed queries @Model.Content.Children.Where(x => x.IsVisible() && x.DocumentTypeAlias != "Category") but I get an error about not being able to use lambda functions with dynamically typed content.

Ideas?

Upvotes: 2

Views: 2081

Answers (1)

Douglas Ludlow
Douglas Ludlow

Reputation: 10922

Two things:

  • The DynamicNode Where clause uses a parameter syntax.
  • Use NodeTypeAlias to check the document type.

Example:

var subs = Model.Children.Where("Visible && NodeTypeAlias != @0", "Category");

Here are a few Umbraco razor resources:

Upvotes: 6

Related Questions