views:

1784

answers:

6

I have been reading the docs and playing with different EventQuery parameters for days now. I am using C# .NET with google's .net api to get the events from a public calendar I set up. I can get the events from the api just fine but I can't get it to give me the next upcoming events by date. My calendar has mixed recurrence events with one-shot events. I have read stuff on the internet to use direct query parameter strings in the request uri but doesn't seem to work right when using it in the .net api structure. Here is what I have currently as my base:

CalendarService myService = new CalendarService("testGoogleCalendar-1");
EventQuery myQuery = new EventQuery();
myQuery.Uri = new Uri(CalendarURI);
myQuery.NumberToRetrieve = NumberOfEvents;
EventFeed calFeed = myService.Query(myQuery);
foreach (AtomEntry entry in calFeed.Entries)
{
    LiteralControl test = new LiteralControl("<p><b>" + entry.Title.Text + "</b></p>");
    this.Controls.Add(test);
}

I have tried playing with the EventQuery's members StartDate, StartTime, EndTime, SortOrder, FutureEvents and even tried adding "?orderby=starttime" to the CalendarURI local member.

The api query's seems to return the order of published date of the event which is when I created the event in the calendar not when the event is going to take place.

I have also been trying to get just the date and time of the event from the AtomEntry object so I can sort it myself and format it with the title in my control but the only place I see it is in AtomEntry's Content.Content which also has other stuff I don't really want. Is there a DateTime member to AtomEntry for this so I can just get the date?

This one has really got me confused right now so any help is appreciated.

Eric

A: 

This was on the Google Doc's for the calendar API:

EventQuery myQuery = new EventQuery(feedUrl);
myQuery.StartTime = new DateTime(2007, 1, 5);
myQuery.EndTime = new DateTime(2007, 1, 7);

EventFeed myResultsFeed = myService.Query(myQuery);

You can read more about it here.

Lucas McCoy
But that doesn't sort the events by date. Yes it gives you the events within that datetime but they are in some random order. I would be fine with that if I could find a way to get the occurance date of the event and sort it myself if I have to.Thanks
I have to agree with Jon here, it seems like the orderby = StartTime should work. Have you tried it?
Lucas McCoy
Yes and no; I have used it in the a broswer test attaching it to the uri and it works but sorts backwards. if i put it in the uri used in the api code then it doesn't work at all. There is also no member to the EventQuery object for Orderby but there IS a member in the EventQuery object for SortOrder; but that doesn't help me either since it is sortying by published date or something.
+1  A: 

I haven't used the GData calendar API from .NET, but I'm pretty familiar with it in Java. The start time of an event will depend on the type of an event. A recurrent event doesn't have any start times as such, but a "single" event may actually have multiple times. These are stored as <gd:when> elements - that's what you need to look for.

It does look like orderby=starttime really should work though. It may be worth using WireShark or something similar to see the exact query going out and the exact results coming back, to check it's not something in the API causing problems - in particular, it could be that using that in the Uri property isn't supported for some reason...

EDIT: Have you tried setting

query.ExtraParameters = "orderby=starttime";

? That's probably the safest way of getting it into the final query uri...

Jon Skeet
I noticed in the calendar demo code that came from google they use an EventEntry instead of an AtomEntry which has a Times member which is of type When. But I played with that and in debugger mode all my objects have a null Times member. I did really think I had something though.If I put the raw uri with that query string in a browser it comes back ok but sorted from most future event to current. they suggest another parameter of sortorder=ascending but that even screws up the browser test.there seems to be so much power here but now way to harness it!thanks i will keep looking
Eric - see my edit; I suspect that adding the extra parameter in the right bit of the query string is the way to go. When you say all your objects had a null Times member - were they single events, or recurrent?
Jon Skeet
I just caught your edit and IT WORKS!!! when i read about the extraparameters they made it sound like my custom key value pairs i coudl store within the calendar.in response to your single or recurrent i checked for both types and they were null.now it would be nice to get the recurring events to show up as single instances instead of a group.i also have to format it better since i can't find the occurance datetime by itself. i guess for now i will just parse the content member and get what i want. Thanks guys!
If you want recurrences to be expanded into single instances, set EventQuery.SingleEvents = true.
Jon Skeet
Beatiful Jon. Thanks a bunch.
+2  A: 

myQuery.ExtraParameters = "orderby=starttime&sortorder=ascending";

Works like a charm!

BerggreenDK
This, as well as using full calendar feed (I'd stupidly been trying to access basic) ended up solving my sortorder as well as populating the EventEntry.When collection.
davewasthere
A: 

Does anyone have any advice on how to get the date and time back out? I do not know how to refer to the <gd:when> element in the C# object.

Alex Burr
A: 

To get the date I did something like this with a Linq Query.

public static IEnumerable FindTopEvent(this AtomEntryCollection items, int cnt) { return (from item in items.OfType() select new EventDto { Url = item.FeedUri, Title = item.Title.Text, Date = item.Times[0].StartTime.ToShortDateString() }).Take(cnt); }

If you have recurring events you may need to look at doing something like this.
//Date = (item.Times.Count > 0) ? item.Times[0].StartTime.ToShortDateString() : ""

Also by setting your SingleEvent property on EventQuery to true it will help.

zachariahyoung
A: 

Does anyone have any idea how to get the start time and end time for an event out of a queries eventfeed? - I'm doing this in vb.net and haven't the slightest idea how to set a reference to "Time" or "when" in order to bind to it in a datalist

DanC