Copied to clipboard

Flag this post as spam?

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


  • Dominic Kelly 114 posts 133 karma points
    Jan 09, 2014 @ 19:14
    Dominic Kelly
    0

    Year filtering via URl

    I'd like to do two things:

    1. Order by custom date (not CreateDate)
    2. Filter by the last URL segment (e.g. december-2014)

    Here is my code:

    @{
      var myList = new List();
      foreach (dynamic page in @Model.AncestorOrSelf(1).DescendantsOrSelf().OrderBy("CreateDate desc").Where("publishDate.Year == 2014")) {
        if(page.NodeTypeAlias == "LatestUpdate")
        { 
          myList.Add(page);
        }
      }
    }
    
    @{
      foreach(dynamic page in myList) {
        

    @page.pageName

    @page.CreateDate.ToString("D")

    @page.landingPageSummary

    More

    } }

     

    1) Sorting by CreateDate is not ideal for importing old content as it can't be in the past. So I've added a new filed called publishDate. But 

    .OrderBy("publishDate desc")

     

    Throws an error. Any idea how to sort by custom date?

    2) I want:

    .Where("publishDate.Year == 2014")

     

    To be dynamic, preferably from the last url segment, which may be december-2014, or january-2013

    I'd rather not use query strings as the URLs are dynamically generated elsewhere and adding the query string parameter would be hard.

    Thanks!

     

     

     

     

     

  • Dominic Kelly 114 posts 133 karma points
    Jan 10, 2014 @ 09:37
    Dominic Kelly
    0

    Anyone? I'm trying to add a archive by year feature to my site, but I can't believe how difficult it is to achieve such a simple and common design pattern.

  • lothar 25 posts 99 karma points
    Jan 10, 2014 @ 17:42
    lothar
    0

    I believe sorting and filtering with custom properties can be achieved by:

    var filteredNodes = new DynamicNode(Model.Id).AncestorOrSelf(1).
                            DescendantsOrSelf().Items.
                            OrderByDescending(x => !string.IsNullOrWhiteSpace(x.GetPropertyValue("publishDate")) ? DateTime.Parse(x.GetPropertyValue("publishDate")) : (DateTime?)null).
                            Where(x => !string.IsNullOrWhiteSpace(x.GetPropertyValue("publishDate")) && DateTime.Parse(x.GetPropertyValue("publishDate")).Year == 2014);
    

    Don't forget to add the using statement in the razorscript at the top to use Linq:

    @using System.Linq
    

    As for getting the filter out of the url, you can access the url using

    var url = HttpContext.Current.Request.Url;
    

    Which is an Uri object: http://msdn.microsoft.com/en-us/library/system.uri%28v=vs.110%29.aspx

    Take a look at the class properties, one of them should be able to get and parse the data out of the url.

  • Dominic Kelly 114 posts 133 karma points
    Jan 10, 2014 @ 18:19
    Dominic Kelly
    0

    Thanks for the reply. That seems like such a lot of code for such a simple function. I decided to remove all sorting params and ask the editor to organise items in the Site tree according to date. I've managed to get the year from the URL, but can't quite figure out how to pass in the variable into the loop?

    @{
    
      // Get year from URL
    
      var url = HttpContext.Current.Request.Url;
      var year = url.Segments[3].TrimEnd('/');
      string[] yearArray = year.Split('-');
    
      var myList = new List<umbraco.MacroEngines.DynamicNode>();
      foreach (dynamic page in @Model.AncestorOrSelf(1).DescendantsOrSelf().Where("publishDate.Year == 2013")) {
      //foreach (dynamic page in @Model.AncestorOrSelf(1).DescendantsOrSelf()) {
        if(page.NodeTypeAlias == "LatestUpdate")
        { 
          myList.Add(page);
        }
      }
    }

    So I need the equivalent of:

    Where("publishDate.Year == @yearArray[1]")

    Please?

     

     

  • lothar 25 posts 99 karma points
    Jan 12, 2014 @ 18:36
    lothar
    0

    Why not just add the year to the string in the Where method? Like so:

    Where("publishDate.Year == " + yearArray[1])
    
Please Sign in or register to post replies

Write your reply to:

Draft