Copied to clipboard

Flag this post as spam?

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


  • Ridhika 5 posts 25 karma points
    May 18, 2015 @ 12:59
    Ridhika
    0

    Alphabetically auto-sorting media item child nodes

    I am trying to auto-sort a child nodes of a media item alphabetically

    i.e. Whenever I add a media item child node, it gets added to the bottom of the list. However, I want that after I save the media item child node; the list should automatically get alphabetically updated(updated by property "givenName"). I am attaching the code snippet I have created so far. Please let me know if this is correct?

     

     public class AutoSort : ApplicationEventHandler

        {

            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,

                ApplicationContext applicationContext)

            {

                MediaService.Saved += MediaService_Saved;

           }

            private void MediaService_Saved(IMediaService sender, SaveEventArgs<IMedia> e)

            {

                try

                {               

                    foreach (var mediaitem in e.SavedEntities)

                    {

                        var parentnode = mediaitem.Parent();

                        var parentid = parentnode.Id;

                        var childnodes = parentnode.Children();

                        var staffprjnodes = new List<StaffPrjNode>();

                        var staffnewprjnodes = new List<StaffPrjNode>();

     

                        foreach (var childnode in childnodes)

                        {

                            int childid = childnode.Id;

                            string givenName = "";

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

                            if (!string.IsNullOrEmpty(name))

                            {

                                staffprjnodes.Add(

                                    new StaffPrjNode

                                    {

                                        Id = childid,

                                        GivenName = name

                                    });

                            }

                        }

     

                          if (staffprjnodes.Count > 1)

                            {

                                var sortorder = "";

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

     

                                foreach (var staffnewprjnode in staffnewprjnodes)

                                    sortorder += staffnewprjnode.Id + ",";                         

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

                              nodesorter.UpdateSortOrder(parentid,sortorder.Trim(','));

                            }

                    }

                   }

                    catch(Exception)

                    {

                        Logging.LogMessage("Not working");

                    }

                }

     

            }

     

            public class StaffPrjNode

            {

                public int Id { get; set; }

     

                public string GivenName { get; set; }

            }

     

     

     

        }

  • 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