Click here to Skip to main content
12,450,977 members (23,551 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked

Get Twitter Trends on Windows Phone 7

, 21 Feb 2011 Ms-PL
Rate this:
Please Sign up or sign in to vote.
In this post, I will show you how you can get the list of trends on Twitter.

In this post, I will show you how you can get the list of trends on Twitter.
In the next post, I’ll show you how you can get the twits related to these trends and generally search twitter for twits.


What The Trend?

Twitter provides a cool site named where you can see the 10 topics which are most discussed currently on twitter.

This site also provides us a web service that returns its results in JSON data format.
The service can be accessed using the following URL:

where {0} should be replaced with your generated application key.

Generate Application Key

Since the twitter trends service provider limits its access using an application key, you must create one.

To do this, go to the site, on the right side, you have a section titled "Request API key". By filling a small form, you can immediately get a free application key which you can use.


Parsing JSON Data

The data returned from is in JSON data format. We would like to parse this data and convert it to C# classes.

To do so, we will use the class DataContractJsonSerializer which resides in the assembly System.ServiceModel.Web.dll.
The first thing we need to do is to add a reference to this assembly.


The service returns the following JSON formatted data:

{"api_version":"2.0","as_of":"2011-02-05 14:39:19","trends":[{"name":"#scariestwordsever","trend_id":"201146","url":"http:\/\/\/search?q=%23scariestwordsever","category_id":"9","category_name":"Meme","query":"
#scariestwordsever","description":{"created_at":"2011-02-04T09:05:27+00:00","blurb_id":"322542","text":"Users are tweeting words\/phrases that would be the scariest to ever hear.","score":"1"},"first_trended_at":"2011-02-04T05:27:55+00:00","last_trended_at":"2011-02-05T14:35:01+00:00","newly_trending":"0","trend_index":"2","locked":false},{"name":"#thebadbehaviourtour","trend_id":"201678","url":"http:\/\/\/search?q=%23thebadbehaviourtour","category_id":"14","category_name":"Other","query":
"#thebadbehaviourtour","description":{"created_at":"2011-02-05T11:56:09+00:00","blurb_id":"322813","text":"@PlanetJedward aka John and Edward have announced new tour dates! @planetJedward are reply to as many fans as they can who put #TheBadBehaviourtour.","score":"0"},"first_trended_at":"2011-02-05T09:18:50+00:00","last_trended_at":"2011-02-05T14:35:01+00:00","newly_trending":"0","trend_index":"3","locked":false},

Yea, this looks scary, but I’ve highlighted the interesting parts.
What we have here is a structure that has the properties:

  • api_version
  • trends
  • as_of

The type of the trends property is an array of structures, each has the following properties:

  • name
  • trend_id
  • url
  • category_id
  • category_name
  • query
  • description
  • first_trended_at
  • last_trended_at
  • newly_trending
  • trend_index
  • locked

The description property is itself a structure that has the following properties:

  • created_at
  • blurb_id
  • text
  • score

All we need to do to get this information in an easy C# way is to define model classes which will have the same properties.

/// <span class="code-SummaryComment"><summary>

to parse the data into these objects, we use the DataContractJsonSerializer:

DataContractJsonSerializer dataContractJsonSerializer = 
	new DataContractJsonSerializer(typeof(TrendsResults));
TrendsResults trendsResults = 

where stream is a Stream object which contains the data in JSON format.

Implement the Get Trends Service

Now that we know how to parse the results, let's wrap this all in a method.

Our GetTrends method will receive a few delegates as parameters to allow our class to work asynchronously:

  • Action<IEnumerable<Trend>> onGetTrendsCompleted, which will be called when the trends are ready to be processed.
  • Action<Exception> onError, which will be called if there is an error while getting the trends.
  • Action onFinally, which will be called always, whether there was an exception or not. Think of it as the finally section on a common try-catch block.

So the method signature will be:

public static void GetTrends(Action<IEnumerable<Trend>> 
	onGetTrendsCompleted = null, Action<Exception> onError = null, 
	Action onFinally = null)

To get the trends, we will use the WebClient class:

WebClient webClient = new WebClient();

// register on download complete event
webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e)

webClient.OpenReadAsync(new Uri(string.Format
	(WhatTheTrendSearchQuery, WhatTheTrendApplicationKey)));

Where WhatTheTrendSearchQuery is defined as follows:

private const string WhatTheTrendSearchQuery = 

The rest of the code handles the different delegates: onGetTrendsCompleted, onError, onFinally. I bring here the method in its full:

/// <span class="code-SummaryComment"><summary>

Using the Twitter Trends Service

As usual, the end result is very easy to use, simply call TwitterService.GetTrends static method and pass a delegate for the “completed” notification.

   (items) => { listbox.ItemsSource = items; },
   (exception) => { MessageBox.Show(exception.Message); },

There is a sample application which can be downloaded here.

Note: This code was first published as part of the “Using Pivot and Panorama Controls” lab found in the Windows Phone Training Kit for Developers, which I wrote for Microsoft.

That’s it for now,
Arik Poznanski.

kick it on Shout it


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Arik Poznanski
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

You may also be interested in...


Comments and Discussions

GeneralMy Vote of 5 Pin
RaviRanjankr28-Feb-11 17:49
memberRaviRanjankr28-Feb-11 17:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 22 Feb 2011
Article Copyright 2011 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid