Tuesday, February 1, 2011

How to sort a YouTube Query with the .NET API SDK

Having trouble sorting the .NET YouTube API video feed?

Countless sites will tell you than in order to sort your videos by the date they were published, all you need to do is set the query.OrderBy property or in some instances the query.setOrderBy property to Google.GData.YouTube.YouTubeQuery.OrderBy.Published.

While working with Intellisense, I noticed that no such values are provided by the OrderBy method. I attempted just using a string query.OrderBy = "Published" but got an error "". Likewise, I thought maybe it's a numerical Enum type and attempted query.OrderBy = 1, but still no luck.

Finally after doing a bit of trial and error, I got it to work with the string "published" (all lowercase).

query.OrderBy = "published"

You can order your videos by a couple different fields, but the string must match these options case sensitvely:

relevance
published
rating
viewCount
Notice: viewCount has an uppercase C