Copied to clipboard

Flag this post as spam?

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


  • hetaurhet 245 posts 267 karma points
    Dec 18, 2011 @ 14:17
    hetaurhet
    0

    accessing all nodes

    I am using umbraco 4.7 and new to this.

    I want to loop through all nodes from the root node, access last updated date of node and compare it with today's date, how can I do that?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 18, 2011 @ 14:58
    Sebastiaan Janssen
    0

    In order to traverse all of your nodes, consider this traverse helper method that I use for creating a sitemap (you may want to remove maxLevelForSitemap):

    <div id="sitemap"> 
        @traverse(@Model.AncestorOrSelf())
    </div>

    @helper traverse(dynamic node){ var maxLevelForSitemap = 4; var values = new Dictionary<string,object>(); values.Add("maxLevelForSitemap", maxLevelForSitemap) ; var items = node.Children.Where("Visible && Level <= maxLevelForSitemap", values); <ul> @foreach (var item in items) { <li><a href="@item.Url">@item.Name</a> @traverse(item) </li> } </ul> }
    Now in the @foreach you can do some date comparison (item.CreateDate is the one your probably want to compare against). This should output the result of "is the create date earlier than today":
    @(item.CreateDate.Date < DateTime.Now.Date)
    Make sure to use 4.7.1 for this to work properly by the way, I can't guarantee this will work in 4.7.0.
  • hetaurhet 245 posts 267 karma points
    Dec 20, 2011 @ 06:58
    hetaurhet
    0

    Ok... a bit helpfule for me. I changed the code as follows for 4.7.0 ...

    <div>
        @traverse(@Model.AncestorOrSelf())
    </div>

    @helper traverse(dynamic node){
      var maxLevelForSitemap = 4;

      var items = node.Children.Where("Visible");
      <ul>
      @foreach (var item in items)
        {
         if(DateTime.Now.Date>item.UpdateDate.Date)
         {
        
         <li><a href="@item.Url">@item.Name</a>
                        @traverse(item)
         </li>
         }
        }
      </ul>
      
    }

    I actually just want to compare the difference between today's date and updated date to be less than 7.... so I tried as follows but it is giving razor error.

    if(DateTime.Now.Date-item.UpdateDate.Date <= 7)

    so what is the mistake in above line? .. is it like I have to format value to just date rather than datetime.. if so then how can I do that?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 20, 2011 @ 08:08
    Sebastiaan Janssen
    0

    You're subtracting dates, so you're getting a full date back, which can't be compared to the int 7. Try this:

    if(DateTime.Now.Date.Subtract(item.UpdateDate.Date).Days <= 7) { //do something }
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Dec 20, 2011 @ 08:13
    Sebastiaan Janssen
    0

    Beware though, that you are stopping traversal of the children of items that are older than 7 days:

    - Home (3 days old)
      - Page 1 (8 days old)
          - Page 2 (2 days old)
      - Page 3 (4 days old)
          - Page 4 (3 days old)

    In this case, Page 1 is too old, so Page 2 will never be shown.

    So you may want to do something like (this is completely untested):

     <ul>
    @foreach (var item in items
        {
         if(DateTime.Now.Date.Subtract(item.UpdateDate.Date).Days <= 7)
         {
         
          <li><href="@item.Url">@item.Name</a </li>
         }
    @traverse(item)
        }
    </ul>
  • hetaurhet 245 posts 267 karma points
    Dec 20, 2011 @ 10:50
    hetaurhet
    0

    yes.. thats what I want..

Please Sign in or register to post replies

Write your reply to:

Draft