Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Doogie Talons 183 posts 318 karma points
    Jan 22, 2016 @ 15:53
    Doogie Talons
    0

    Create a list of nodes based on a datepart of CreateDate

    Hi Guys, doe's any one know how to achieve the following.

    Using another post I have managed to create an archive of unique months and years from a list of articles.

    However I don't know how to write the razor query to list the headlines based on what month/year I am at.

    In codeified english it would look like this.

    foreach( var article in nodes.Where( THE YEAR PART OF CreateDate IS EQUAL TO yearVariable AND MONTH PART OF CreateDate IS EQUAL TO THE monthVariable) ) {

    Headline

    }

    Could anyone help with with the code needed to list the articles based on a given year month of createdate.

    Kind Regards

    Doogie

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 23, 2016 @ 14:00
    Alex Skrypnyk
    1

    Hi Doogie,

    Did you try like that:

    foreach (var article in nodes.Where(x => x.CreateDate.Year == 2012 && x.CreateDate.Month == 7))
    

    Best

  • Doogie Talons 183 posts 318 karma points
    Jan 25, 2016 @ 11:13
    Doogie Talons
    0

    I have used similar before getting a course name from a list of courses. In this case I tried your code with the variables in and got the following error.

    "Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"

    I am looking up what this means but it's still open.

    Thanks for your help.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 25, 2016 @ 11:17
    Alex Skrypnyk
    0

    Hi Doogie,

    You can use strongly typed nodes. Can you show your current code ?

    Thanks

  • Doogie Talons 183 posts 318 karma points
    Jan 25, 2016 @ 11:49
    Doogie Talons
    0
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{  
    var blogNode = Umbraco.Content(1147);
    var articles = blogNode.Descendants("Article").OrderBy("CreateDate");   
    int lastYear = 0;
    int lastMonth = 0;
    }
    
    <div class="archive">
    <h2>Archive</h2>
    <ul class="archivenav">
    @foreach (var e in articles)
    {
        int year = e.CreateDate.Year;
        int month = e.CreateDate.Month;
    
        if (year > lastYear)
        {
        <li class="toggle-trigger">
        @year
        </li>
        }
    
        if (year != lastYear || month > lastMonth)
        {
            <div class="toggle-box" style=""> 
              <li class="toggle-trigger">
                  @e.CreateDate.ToString("MMM")
              </li>
    
            <div class="toggle-box" style="">
    
                @{
                var yearv = year;
                var monthv = month; 
                var nodes = articles.Where(x => x.CreateDate.Year == yearv && x.CreateDate.Month == monthv);
                 }
    
    
                @foreach( var node in nodes)
                {
                <li>TEST</li>
                }
    
    
    
            </div>  
            </div>
    
    
    
        }
    
        lastYear = year;
        lastMonth = month;
    
    }
    </ul>
    </div>
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 25, 2016 @ 14:39
    Alex Skrypnyk
    100

    The only change you need to do is

    var blogNode = Umbraco.Content(1147);

    change to:

    var blogNode = Umbraco.TypedContent(1147);

    After that everything is working fine. I don't like dynamic type at all.

    Thanks,

    Alex

  • Doogie Talons 183 posts 318 karma points
    Jan 25, 2016 @ 15:13
    Doogie Talons
    0

    Cool after your wee change above I changed the test bit to.

    @foreach( var node in nodes)
                {
                var title = node.GetPropertyValue<string>("copyHeader");
                <li>@title</li>
                }
    

    And everything works fine.

    Thanks very much Alex.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 25, 2016 @ 15:14
    Alex Skrypnyk
    0

    Doogie, you are welcome !

  • Doogie Talons 183 posts 318 karma points
    Jan 25, 2016 @ 15:20
    Doogie Talons
    0

    Ok for anyone interested I have cleaned it up a bit as the second set of variables were for me to check where the hell it broke :)

    What this does is create a list of article headlines by YEAR - MONTH without having to use date folders. The styles are related to an accordion but could be just UL LI etc. and the node number is the parent node of a bunch of articles with the page type Article.

    Thanks again.

    Rob

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @{  
        var blogNode = Umbraco.TypedContent(1147);
        var articles = blogNode.Descendants("Article").OrderBy("CreateDate");   
        int lastYear = 0;
        int lastMonth = 0;
        }
        <div class="archive">
        <h2>Archive</h2>
        <ul class="archivenav">
        @foreach (var e in articles)
        {
            int year = e.CreateDate.Year;
            int month = e.CreateDate.Month;
            if (year > lastYear)
            {
            <li class="toggle-trigger">
            @year
            </li>
            }
            if (year != lastYear || month > lastMonth)
            {
            <div class="toggle-box" style=""> 
              <li class="toggle-trigger">
                  @e.CreateDate.ToString("MMM")
              </li>
                <div class="toggle-box" style="">
                    @foreach( var node in articles.Where(x => x.CreateDate.Year == year && x.CreateDate.Month == month))
                    {
                    var title = node.GetPropertyValue<string>("copyHeader");
                    <li>@title</li>
                    }
                </div>  
             </div>
            }
            lastYear = year;
            lastMonth = month;
        }
        </ul>
        </div>
    
Please Sign in or register to post replies

Write your reply to:

Draft