Copied to clipboard

Flag this post as spam?

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


  • Chris Norwood 101 posts 546 karma points
    Mar 31, 2020 @ 09:21
    Chris Norwood
    0

    Selecting by Date using ContentAtXPath

    Hi Umbracians!

    I'm struggling with getting a list of items via an XPATH expression involving a date and was wondering if anybody had managed it - I have code that looks like this:

    var xpath = "//newsItem[@isDoc]";
        var newsItemsXpath = Umbraco.ContentAtXPath(xpath).Cast<ContentModels.NewsItem>();
    

    This selects all news items, but I'd like to select only those published in the last 30 days or so; I've googled for solutions and found various suggestions to use e.g. xs:date(@datePublished), but when I tried this:

    var fromDate = DateTime.Today.AddDays(-30);
    
        var xpath = string.Format("//newsItem[xs:date(datePublished) > '{0}'][@isDoc]", fromDate.ToIsoString());
        var newsItemsXpath = Umbraco.ContentAtXPath(xpath).Cast<ContentModels.NewsItem>();
    

    Umbraco threw an error (an invalid token) - I suppose there might be invalid data in datePublished)?

    News Items have a data type called DatePublished attached, so I'm just trying to select by that - has anybody managed to select by a date using XPath?

    Thanks in advance,

    Chris.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies