Copied to clipboard

Flag this post as spam?

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


  • Paul Wright 16 posts 97 karma points
    Nov 05, 2019 @ 11:13
    Paul Wright
    0

    Getting Items Order via Start date instead of Created

    Morning all,

    Just got a quick question, I am setting up an Events page on our site which would allow us to add Events to a calendar but also need to be displayed in a list. no the function is working fine and displaying in both the calendar and also the list but the ordering of the list is via the date of the creation and not by the start date of the event.

    View: enter image description here as you can see the order is not showing correctly

    does anyone know what i need to add to order the events via start date instead of created date? I'm guessing i just need to add some ordering onto the for each but really unsure.

    here is my currently code

    @{
        var eventId = 1131;
    
        var eventPage = Umbraco.Content(eventId);
    }
    
    
     @foreach(var post in eventPage.Children)
                                {
                                            if(post.Value<DateTime>("startDate") > DateTime.Now){
    
                                                IPublishedContent image = post.Value<IPublishedContent>("eventThumbnail");
    
                                                string day = post.Value<DateTime>("startDate").ToString("dd");
                                                string month = post.Value<DateTime>("startDate").ToString("MMM");
    
                                                string start = post.Value<DateTime>("startDate").ToString("dd MMMM yyyy hh:mm tt");
                                                string end = post.Value<DateTime>("endDate").ToString("dd MMMM yyyy hh:mm tt");
                                           <div class="col-md-12" style="Padding-bottom: 15px;">     
                                            <div class="homepageEvents">
                                                <div class="event clearfix">
                                                    <h3>@post.Value("title")</h3>
                                                    @*<img src="@image.Url" height="150" width="150" />*@
                                                    <div class="event-box">
                                                        <div class="eventDay">@day</div>
                                                        <div class="eventMonth">@month</div>
                                                    </div>
                                                    <p>@post.Value("eventSummary")</p>
                                                    <a class="btn btn-lr" href="@post.Url">More Details</a>
                                                    @*<div class="eventDates">
                                                        Start: <span style="margin-right:20px" class="startDate">@start</span><br />
                                                        End: <span style="margin-right:20px" class="startDate">@end</span>
                                                    </div>*@
                                                </div>
                                            </div>
                                        </div>
                                        <br />
                                            }
    
                                }
    
  • Paul Griffiths 370 posts 1021 karma points
    Nov 06, 2019 @ 11:36
    Paul Griffiths
    0

    Hi Paul,

    If you are using Umbraco 8 then you should be using .Value instead of GetPropertyValue with IPublishedContent I believe.

    If my understanding is correct then you could either apply an OrderByDescending on the children on the foreach collection or create a new variable to hold a processed collection i.e. hold events that have passed and in order (see bottom code example)

    Depending on whether you have models builder enabled and whether your events page model is cast etc. you have some options i think.

    Models builder which is cast to type e.g. (@inherits Umbraco.Web.Mvc.UmbracoViewPage< EventsPage>)

        @foreach(var post in eventPage.Children.OrderByDescending(x => x.StartDate)
    

    Models builder which is IPublishedContent (@inherits Umbraco.Web.Mvc.UmbracoViewPage)

        @foreach(var post in eventPage.Children.OrderByDescending(x => x.GetPropertyValue<DateTime>("startDate"))
    

    You could also a where clause to the LINQ statement to filter out any events that have passed e.g. (Umbraco 8 example)

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<EventsPage>
    @{ 
    
       //store the sorted\filtered list of events
        var events = Model.Children
                    .Where(x => x.Value<DateTime>("startDate") >= DateTime.Now)
                    .OrderByDescending(x => x.Value<DateTime>("startDate"));
    
        foreach (var item in events)
        {
            //process each item
        }
    }
    

    I'm currently on the train so my code examples may need tweaking. Also, take a look at which details the differences between dynamic and strongly typed access in v7 (https://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/)

    Hopefully i've understood your issue and my response helps.

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft