Twitter from ASP.NET – Retrieving Statuses

Retrieve Twitter Status Messages 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.


Related Post

3 Responses to “Twitter from ASP.NET – Retrieving Statuses”