0

I am getting error in a Umbraco 7.0.3 macro which traverse through all the childs and returns little more than a list. To start with I have used Umbraco back office to build the basic navigation and side menu items. Meanwhile once the local environment is setup I started working locally.

The issue is, the code I had build using back office for navigation works fine but same code does not work if the items are created in VS2012 Ultimate version, even i just paste the same code from original working navigation macro.

I am getting following error : 'Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Any', suggesting that the page list is dynamic. To my amusement, the same code work for existing navigation, then why not with new items? Is there any setting in VS2012 where it is marking the file unusable with UTF8 editors or non-valid html?

My question is how can I find count or Any items in a razor macro? I have already tried Enumerable items count and any methods, but no use.

Any pointers on how to find number of items exists will be helpful.

I am providing some more information on Paulo's request. Erroring on "startNode.Children.Where("Visible").Any()" line. Following is the macro code:

@inherits Umbraco.Web.Macros.PartialViewMacroPage
@*
    === Macro Parameters To Create ===
    Show:True   Alias:nodeId Name:Node ID    Type:Content Picker
*@
@if (Model.MacroParameters["startNodeID"] != null)
{
    @* Get the start node as a dynamic node *@
    var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);

    if (startNode.Children.Where("Visible").Any())
    {
        <div class="container">
            <div class="row">
                @foreach (var page in startNode.Children.Where("Visible"))
                { 
                    <div class="col-sm-3 col-md-3">
                        <div class="thumbnail">
                            <img src="~/images/Tiles/300x200.jpg" alt="@page.Name">
                            <div class="caption">
                                <h3><a href="@page.Url">@page.Name</a></h3>
                                <p>@page.GetPropertyValue("summary").Substring(0, 100)</p>
                                <a href="@page.Url" class="btn btn-default">Read More</a>
                            </div>
                        </div>
                    </div>
                }
            </div>
        </div>
    }
}
Veverke
  • 9,208
  • 4
  • 51
  • 95
Sanjay Zalke
  • 1,331
  • 1
  • 18
  • 25
  • I sorry but you haven't researched enough. You don't give much detail about the problem for someone who's not familiar with Umbraco. – Paulo Morgado Mar 18 '14 at 10:04
  • Hey Paulo, I have edited question and add more information, let me know if this makes more sense. – Sanjay Zalke Mar 25 '14 at 13:04
  • What's the type of the return value of `startNode.Children.Where("Visible")`? Is it `IEnumerable`? Does `Enumerable.Any(startNode.Children.Where("Visible"))` work? – Paulo Morgado Mar 25 '14 at 20:13

2 Answers2

7

I have just came across the same issue, and the following worked for me: simply cast your umbraco property to a DynamicPublishedContentList variable (assuming the property is indeed of such type).

Example:

var homePage = CurrentPage.AncestorsOrSelf().Where("DocumentTypeAlias == @0", "yourPageAlias").FirstOrDefault();

var umbracoFolder = homePage.yourUmbracoFolderName;
var umbracoFolderItems = Umbraco.Content(umbracoFolder.ToString());

Umbraco.Web.Models.DynamicPublishedContentList yourList = umbracoFolderItems.yourItems as Umbraco.Web.Models.DynamicPublishedContentList;

yourList.Count() will work.

Update: The above works, but you can make your life easier by working against a List instead of DynamicPublishedContentList, so you have not only the regular IEnumerable extension methods, but also indexers.

List<dynamic> items = new List<dynamic>(umbracoFolderItems.yourItems);
Veverke
  • 9,208
  • 4
  • 51
  • 95
0

I'm guessing you're missing a using System.Linq directive.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • Sorry Paulo, but it did not worked. I believe Umbraco must be adding all the "basic/required" namespaces by default. #don'tknow – Sanjay Zalke Mar 25 '14 at 12:56
  • 2
    I'm assuming there's a `Where` method that takes a string. But looking at the code, it might not be the intention. What are you trying to do? Can you provide non-LINQ equivalent code? – Paulo Morgado Mar 02 '15 at 07:22
  • 1
    This doesn't work because extension methods can't be bound dynamically, but you can verify this very easily: dynamic x = new int[10]; Console.WriteLine(x.Any()). It will fail – Coastpear Apr 23 '15 at 20:19