Twitter from ASP.NET – Retrieving Statuses
So far we’ve established a way to send messages from our ASP.NET application to twitter. But what about retrieving the messages your friends have sent out?
This is actually pretty simple once you know which API call retrieves what.
The fact of the matter is, there are several API calls we can make to retrieve status messages.
- Status.FriendsTimeline() retrieves the messages of our friends.
- Status.Replies() retrieves all of the replies to us
- Status.UserTimeline() retrieves all of the messages a specific user sent out.
The one we want to focus on today is FriendsTimeline().
FriendsTimeline takes an optional parameter of type TwitterParameters which allows you to specify:
- since_id: retrieves all the messages is a specific ID. This is useful if you are already displaying messages and just need the most recent ones since the last update.
- max_id: retrieves all the messages that came prior to a specific message. Useful if you are paging backwards in time.
- count: retrieve a specific number of messages. This number can’t be greater than 200, which is the max number that Twitter will allow you to retrieve.
- page: lets you page through messages to retrieve up to 3200 status messages. For example, page = 2 and count = 200 will retrieve messages 201 – 400.
For this example we will retrieve the last 50 messages. The easiest way to display them will be in a ListView or some other Repeater control and the easiest way to hook up a Repeater control is to use an ObjectDatasource. This means we will need a class that we can specify in our ObjectDatasource.
I created a class called Messages that I put in App_Code. It’s pretty simple code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Twitterizer.Framework; /// <summary> /// Summary description for Messages /// </summary> public class Messages { public Messages() { // // TODO: Add constructor logic here // } public static TwitterStatusCollection Friendtimeline() { String password = HttpContext.Current .Session["Password"].ToString(); String name = HttpContext.Current .User.Identity.Name; TwitterParameters parameters = new TwitterParameters(); parameters .Add(TwitterParameterNames.Count, 100); return (new Twitter(name, password)) .Status.FriendsTimeline(parameters); } }
You notice that I’m using HttpContext to retrieve the username and password because I no longer have access to the Page object.
From here, all you need to do is use the wizards in Visual Studio to
- Create an ObjectDataSource.
- Point the ObjectDataSource to Messages.Friendtimeline()
- Point your repeater control to the ObjectDataSource
The only other thing you’ll probably want to do is force a DataBind if the user just sent a message out. So in the button click event handler that we put in last time, add the following code:
DataList1.DataBind();
Where DataList1 is your repeater control.
If you are interested in Twitter, there are a few products I’ve found useful. One is an e-book that shows some free tools that will help automate the management of your twitter account. You can find out more about it here. If you have ANY interest in increasing the number of followers you have on Twitter, for whatever reason, this is the book to get.
Other post in Twitter
- Twitter from ASP.NET - May 13th, 2009
- Twitter from ASP.NET – Simple GUI - May 14th, 2009
- Twitter from ASP.NET – Retrieving Statuses - May 21st, 2009
- Twitter From ASP.NET – Friends vs Followers - June 4th, 2009
- Friday Books – Twitter Power - June 5th, 2009
- Twitter Follower Obsessions - June 8th, 2009
- Twitter From ASP.NET – Direct Messages - June 15th, 2009
- Filtering the Internet Noise - December 10th, 2009
Other Related Items:
MCTS Self-Paced Training Kit (Exam 70-562): Microsoft .NET Framework 3.5-ASP.NET Application Development: Microsoft(r) .Net Framework 3.5 ASP.Net Application Development (Pro - Certification)Ace your preparation for the skills measured by MCTS Exam 70-562âand on the job. Work at your own pace through a series of lessons and reviews... Read More >
Don't Mess With Me... Programmer on Long Sleeve Women's Cotton T-Shirt (in 9 colors)100% preshrunk heavyweight cotton; double-needle stitching throughout; seamless rib at neck; shoulder-to-shoulder tape; heather grey is 90% cotton, 10% polyester; fashion cut; 5/8" rib collar; fitted tapered sleeve.
If you're new here, you may want to subscribe to the mailing list to get notifications of new post and a virtual tour of past topics. Thanks for visiting!










[...] Twitter from ASP.NET – Retrieving Statuses (Dave M. Bush) [...]
I think open API is one of the success point for Twitter and other SNS. It give people more scale.
[...] the direct messages sent to you is remarkably similar to the code we wrote earlier that retrieved the messages sent by our friends. You’ll need to create a class called “Direct” and add a method to it named [...]