Copied to clipboard

Flag this post as spam?

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


  • Nauman 98 posts 118 karma points
    Mar 11, 2010 @ 10:47
    Nauman
    0

    auto sort nodes by nodedate

    Hi

    I have created child nodes under a parent node "News" and there is "news date" field in each news, which is picked up by date picker when creating a News item.

    In content section of Umbraco, I want to display the node items (news items) descending with the respect to the "news date", How can I do that so umbraco content section displays news items descending respective to the "news date"?

    Regards

    Nauman

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 11, 2010 @ 10:55
    Dirk De Grave
    0

    By default, umbraco appends the node as the last node for a certain parent node. But, you could write an event handler that will do the sorting for you (automated) or do the sort manually (via context menu).

    If you're going the automated route, then write an event handler that fires on document save or document publish and sort the nodes based on the "news date" property of the nodes.

    In your event handler, get all nodes of the same level (having the same parent) and change the sortOrder field in the database for each of the nodes using some sql statements.

     

    Hope this helps.

    Regards,

    /Dirk

  • Nauman 98 posts 118 karma points
    Mar 11, 2010 @ 11:24
    Nauman
    0

    Thanks Dirk, i will give it a try and come back to you, if i need some further help.

    Cheers

    Nauman

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Mar 11, 2010 @ 11:36
    Dirk De Grave
    0

    Can find some nice wiki posts on event handling starting here.

     

    /Dirk

  • Delete me 45 posts 66 karma points
    May 03, 2010 @ 12:35
    Delete me
    0

    Hi Nauman

    I'm trying to do the same as you - did you make it work, and if so, would you be so kind and share a codesnippet with me?

    Thanks a bunch :)

    /Mathias

  • Petr Snobelt 923 posts 1535 karma points
    May 03, 2010 @ 16:03
  • Delete me 45 posts 66 karma points
    May 05, 2010 @ 22:37
    Delete me
    0

    Petr,

    Thanks! Can it be also be used to sort by dates from a "news date" field set by datepicker (as described in the first post)?

    /Mathias

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 06, 2010 @ 00:39
    Aaron Powell
    0

    Keep in mind that doing on-new/ on-save/ etc sorting is very database intensive. 4.1 makes it a bit easier to 'fake' the sort order in the tree, I'm speaking about this in a few weeks and after that I'll put it up on my blog (sorry, no early sneak peaks :P)

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 09, 2010 @ 17:05
    Matt Brailsford
    1

    Maybe a little late, and I'm not 100% on the code, but wanting to do the same, I came up with the following:

    namespace

     

     

    UmbracoDemo

    {

     

     

    public class EventRegistrar : umbraco.BusinessLogic.ApplicationBase

    {

     

     

    public EventRegistrar()

    {

     

     

    Document.AfterSave += new Document.SaveEventHandler(Document_AfterSave);

     

     

    Document.AfterPublish += new Document.PublishEventHandler(Document_AfterPublish);

    }

     

     

    protected void Document_AfterSave(Document sender, umbraco.cms.businesslogic.SaveEventArgs e) { SortNews(sender); }

     

     

    protected void Document_AfterPublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e) { SortNews(sender); }

     

     

    protected void SortNews(Document doc)

    {

     

     

    if (doc.ContentType.Alias == "newsPage")

    {

     

     

    var parent = doc.Parent;

     

     

    if (parent == null)

     

     

    return;

     

     

    var articles = new List<Article>();

     

     

    foreach(CMSNode child in parent.Children)

    {

     

     

    var childDoc = new Document(child.Id);

     

     

    var articleDate = DateTime.MinValue;

     

     

    if (childDoc != null &&

    childDoc.ContentType.Alias ==

     

    "newsPage" &&

     

     

    Convert.ToString(childDoc.getProperty("articleDate").Value) != "" &&

     

     

    DateTime.TryParse(Convert.ToString(childDoc.getProperty("articleDate").Value), out articleDate))

    {

    articles.Add(

     

    new Article

    {

    ID = childDoc.Id,

    Date = articleDate

    });

    }

     

     

    else

    {

    articles.Add(

     

    new Article

    {

    ID = child.Id,

    Date =

     

    DateTime.Now.AddYears(100)

    });

    }

    }

     

     

    if (articles.Count > 1)

    {

     

     

    var sortOrder = "";

    articles.Sort((x, y) => y.Date.CompareTo(x.Date));

     

     

    foreach (var article in articles)

    sortOrder += article.ID +

     

    ",";

     

     

    var nodeSorter = new umbraco.presentation.webservices.nodeSorter();

    nodeSorter.UpdateSortOrder(parent.Id, sortOrder.Trim(

     

    ','));

    }

    }

    }

    }

     

     

    public class Article

    {

     

     

    public int ID { get; set; }

     

     

    public DateTime Date { get; set; }

    }

    }

  • Delete me 45 posts 66 karma points
    May 10, 2010 @ 17:08
    Delete me
    0

    Matt,

    Thanks, its working great!

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 10, 2010 @ 19:06
    Matt Brailsford
    0

    Hey Mathias,

    If this solved your problem, maybe you could mark my post as the answer to help others out.

    I did make one minor change. At the end i now do a check so that I only resort if the sort order has changed.

    When I have a spare moment i'll role it up into a package.

    Many thanks

    Matt

  • Delete me 45 posts 66 karma points
    May 10, 2010 @ 20:39
    Delete me
    0

    Hi Matt

    Would be happy to mark your aswar as a solution, but can't because I didn't start the thread :-(

    Good idea with the check in the end, will try to implement that also.

    /Mathias

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 10, 2010 @ 22:23
    Matt Brailsford
    0

    Ahh, my bad, thought it was your question.

    Ahh well, glad I could help none the less.

    Matt

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 11, 2010 @ 22:08
    Matt Brailsford
    0

    Hey Mathias,

    I've put toegther a package if you'd like to give it a try:

    http://our.umbraco.org/projects/document-sorter

    Let me know how you get on

    Matt

    PS It's my first package so please be gentle

  • Delete me 45 posts 66 karma points
    May 13, 2010 @ 16:26
    Delete me
    0

    Hi Matt,

    Great package, with very good functionality and config options!

    For my inspiration and learning, would you mind provide me the final source code?

    /Mathias

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    May 14, 2010 @ 09:33
    Matt Brailsford
    0

    Hi Mathias,

    I've added the source code to the project page.

    Many thanks

    Matt

  • Ridhika 5 posts 25 karma points
    May 15, 2015 @ 10:09
    Ridhika
    0

    Please help me how to alphabeticaly sort media nodes in Umbraco 6. Here is the code snippet showing the work I have done so far:-

     void MediaServiceSaved(IMediaService sender, SaveEventArgs e)

          {

              try

              {              

                  foreach (var mediaitem in e.SavedEntities)

                  {

                      var parentnode=  mediaitem.Parent();

                      var parentid = parentnode.Id;

                      var childnodes = parentnode.Children();        

     

     

                      var staffprjnodes = new List< TestNode >();

                       var staffnewprjnodes = new List< TestNode >();

                      foreach (var childnode in childnodes)

                      {

                          int childid = childnode.Id;

                          string name = childnode.Properties.Where(property => property.Alias == "Name").ToString();

                          if (!string.IsNullOrEmpty(name))

                          {

                              staffprjnodes.Add(

                                  new StaffPrjNode

                                  {

                                      Id = childid,

                                      Name = name

     

                                  });

                          }

                      }

                      var sortorder = "";

                        if (staffprjnodes.Count > 1)

                          {

                              var sortorder = "";

                             staffnewprjnodes= staffprjnodes.OrderBy(x => x.Name).ToList();

                          }} 

                  }}

              catch (Exception)

              {        

                  throw;

              } } }

     

        public class TestNode

        {

            public int Id { get; set; }

            public string Name { get; set; }

        }

     Please review this code. The sorted list of nodes will be in the list staffnewprjnodes.Please tell me how to "updatesortorder" of the parent node with this sorted list?

     

     

           

     

     

  • Ridhika 5 posts 25 karma points
    May 18, 2015 @ 07:41
    Ridhika
    0

    Guys...please can anyone suggest ...am I on the right track or not?

Please Sign in or register to post replies

Write your reply to:

Draft