Google Calendar api v3 - Sorting Events by Start (DateTime)

188 views
Skip to first unread message

Supreet Kaur

unread,
May 6, 2016, 12:13:25 PM5/6/16
to Google Calendar API

I am using a data repeater to pull events from a public google calendar using the google calendar api v3 into a webpage. I want to sort the events listed in the data repeater by the event start time. I would like to sort the events listed by start date and start time, right now it looks like the events are being sorted by last updated item.


Below is the code I am using to pull the events from my calendar to my webpage


Webpage
https://stampunion.umd.edu/OMSE/


aspx.cs

private void GetGCalEvents()
{
   
var service = new CalendarService(new BaseClientService.Initializer()
   
{
       
ApiKey = "My API Key",
       
ApplicationName = "Calendar Name",
   
});

   
var events = service.Events.List("Public Google Calendar").Execute();

   
DataTable dt = new DataTable();
    dt
.Columns.Add("Summary");

   
foreach (var myEvent in events.Items)
   
{
        dt
.Rows.Add(string.Format("<strong>Tutor: " + myEvent.Summary + "</strong><br />" + myEvent.Start.DateTime.ToString() + " - " + myEvent.End.DateTime.ToString() + "<br /><strong>Courses</strong>: " + myEvent.Description));
   
}

    rptCalEvents
.DataSource = dt;
    rptCalEvents
.DataBind();
}

aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   
<title></title>
</head>
<body>
   
<form id="form1" runat="server">
   
<div>
       
<asp:Repeater runat="server" ID="rptCalEvents">
           
<ItemTemplate>                
               
<font style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000; font-size: 20px;">
                   
<%# Eval("Summary") %>
               
</font>                        
           
</ItemTemplate>
           
<SeparatorTemplate>
               
<hr />
           
</SeparatorTemplate>
       
</asp:Repeater>
   
</div>
   
</form>
</body>
</html>

Supreet Kaur

unread,
May 9, 2016, 5:14:01 PM5/9/16
to Google Calendar API
I figured out a solution to my problem. I'm posting it up here for anyone else who might have a similar problem and need to find a solution.

Webpage

https://stampunion.umd.edu/OMSE/

aspx.cs
private void GetGCalEvents()
{  
   
var service = new CalendarService(new BaseClientService.Initializer()
   
{
       
ApiKey = "My API Key",
       
ApplicationName = "Calendar Name",
   
});


   
EventsResource.ListRequest request = service.Events.List("Public Google Calendar ID");

    request
.SingleEvents = true;
    request
.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
   
Events events = request.Execute();


   
DataTable dt = new DataTable();
    dt
.Columns.Add("Summary");

   
foreach (var myEvent in events.Items)
   
{

        dt
.Rows.Add(string.Format(myEvent.Start.DateTime + " - " + myEvent.End.DateTime + "<br /><strong>Event Title: " + myEvent.Summary + "</strong>" + "<br /><strong>Event Description: </strong>" + myEvent.Description));
   
}
   
    rptCalEvents
.DataSource = dt;
    rptCalEvents
.DataBind();
}
Reply all
Reply to author
Forward
0 new messages