How to Display Your Twitter Feed using ASP.NET

UPDATE 3/23/2010 – Ricky from Twitterizer commented below noting that basic authentication will soon go away via Twitter and OAUTH will be required. Note that the code below will only work for a few months. We will post an updated code-example soon.

As I write this article, It’s about 75 degrees and Sunny outside. When I should be going out on a bike ride, instead I’ve opted to play with Twitterizer (an ASP.NET Twitter Library). Twitterizer is an ASP.NET library that lets you interact with the Twitter API using easy to use objects and methods. It will work with any of the .NET variants (C#, VB, J#, Windows Forms, ASP.NET, WPF, etc). I added the functionality into the 360 Web Content Management System and I thought I’d share with you how I did it.

Here’s how to retrieve twitter feeds in ASP.NET

(1) Get a copy of the Twitterizer Library

First, you’ll need to get a copy of the Twitterizer Library from Google’s Codebase. The download is pretty small and contains only the application library (DLL) you need. Create a new website in ASP.NET and extract the twitterizer library to the /bin/ folder so that you can use it.  Once you have it placed in your /bin/ folder, add a “using” reference to the library in the header of your page.

using Twitterizer.Framework;

(2) Create a “Twitter” object and Retrieve Your Status Updates.

The library contains a few different objects that you can create. A “Twitter” object is the most generic object that you can create. Creating an instance of this object using your username and password gives you all the functionality you would normally have in Twitter, but instead of using the Twitter web interface, you’re using C# or Visual Basic. First, we’ll need to instantiate the object, and then get a collection of status updates from your account.

Twitter thisUser = new Twitter(“UserNameHere”, “PasswordHere”);
TwitterStatusCollection thisCollection = thisUser.Status.UserTimeline();

(3) Loop Through Your Status Updates and Generate Some HTML

The “TwitterStatusCollection” object type is a list of “TwitterStatus” objects, so you can use a foreach loop and go through your most recent status updates. You’ll notice in the code below that I also do some basic work with the time of the status update to generate a hyperlink to the page of the status, similar to what Twitter does.

string TwitterCode = “”;
foreach (TwitterStatus thisStatus in thisCollection)
{

TimeSpan thisSpan = new TimeSpan();
thisSpan = DateTime.Now.Subtract(thisStatus.Created);

string TimeBetween = “”;
if (thisSpan.Days > 0) { TimeBetween = thisSpan.Days.ToString() + ” days ago”; }
else if (thisSpan.Hours > 0) { TimeBetween = thisSpan.Days.ToString() + ” hours ago”;}
else if (thisSpan.Minutes > 0) { TimeBetween = thisSpan.Days.ToString() + ” minutes ago”;}
else if (thisSpan.Seconds > 0) { TimeBetween = thisSpan.Days.ToString() + ” seconds ago”;}

TwitterCode += “<div class=’TwitterStatus’>” + thisStatus.Text + ” <a href=’http://twitter.com/” + thisStatus.TwitterUser.UserName + “/status/” + thisStatus.ID + “‘>” + TimeBetween + “</a></div>”;
}
(4) Display Your Tweets

You now have a string with your most recent twitter status updates that you can display on the page using a simple Response.Write() or you can display it in a label. You can see a variation of this code running on the “Twitter” page for the 360 Web Content Management System.

You can also download a copy of my sample code.

Comments

  1. Mushtaq says:

    execellent , it was up to my needs..

    Thanks for the post

  2. Can the feeds be private, so the viewer as to enter their twitter password the first time they view the embedded feeds?

  3. Thanks Matthew, you have just saved me several hours of xml headaches by showing me this code.

    Great post

  4. vijayamalla says:

    Hey, I really liked the code that put in for the Twitter.
    but there is a problem with the time, i used the DateTime.Now but when i debugged, the status variable is 4hrs ahead to the time it was created., can you please help with this?
    is that some timezone thing?

  5. Where you have:

    twStatus.TwitterUser.UserName

    I was actually getting my display name instead of my username, so my URLs were twitter.com/Jeremy Coulson instead of twitter.com/mrcoulson. I replaced that with:

    twStatus.TwitterUser.ScreenName

    Now it’s successful. Thanks for this information! I didn’t understand how to actually display the tweets until I saw this stuff.

    Jeremy

  6. Great post, this saved me a bit of time and work, but I was wondering if there’s a quick way to display only the last five updates? I’ll keep poking around and if I find something, post the solution.

  7. Kartikeya says:

    I am using Twitterizer and my code is as follow:

    string uname = “test@test.com”;
    string passwd = “XXXXXX”;
    Twitter t = new Twitter(uname, passwd);
    TwitterParameters paras = new TwitterParameters();
    paras.Add(TwitterParameterNames.Count, 10);
    TwitterStatusCollection tsc = t.Status.UserTimeline(paras);

    foreach (TwitterStatus tStatus in tsc)
    {
    strResponse = strResponse + tStatus.Text + “” + tStatus.Created + “” ;
    }

    Now while getting the text using tStatus.Text , how can I create the link over url that iam getting in response. Right now it’s displaying the text in plain format.
    e.g., Check out Facebook viewed as riskiest social network by companies http://goo.gl/fb/s4Fw .

    Is there is any way to get the inner html text in response?

  8. Thanks for the great write up on my little project. :)

    Twitterizer is undergoing a complete re-write, since BASIC authentication is going away in favor of OAuth. I’m trying to let everyone know myself so that stale tutorials aren’t left all over the place, since these kinds of samples will stop working in June.

    If you have any questions about the new version, feel free to contact me directly (be sure to mention this post so I know how you found me). If you make a follow up tutorial, let me know so I can link to your site on our new tutorials page.

  9. Ricky – Thanks for the update about the API status change. I’ll put a note on the post and write an updated example using the new authentication methods.

  10. Thanks for this, I found it a great help. I’m having trouble with controlling the amount of Tweets that appear on screen. How can i limit this to only 3 or 4 of the latest. Also my times are only showing 0secs or 0 hours. Please excuse me as I’m new to ASP.net

  11. Hi Brian,

    You will want to use a for loop instead of a foreach loop, which will look something like this:

    int TweetLimit = thisCollection.Count;
    if(TweetLimit > 4) {TweetLimit = 4; }

    string TwitterCode = “”;
    for(int x=0; x 0) { TimeBetween = thisSpan.Days.ToString() + ” days ago”; }
    else if (thisSpan.Hours > 0) { TimeBetween = thisSpan.Days.ToString() + ” hours ago”;}
    else if (thisSpan.Minutes > 0) { TimeBetween = thisSpan.Days.ToString() + ” minutes ago”;}
    else if (thisSpan.Seconds > 0) { TimeBetween = thisSpan.Days.ToString() + ” seconds ago”;}

    TwitterCode += ” + thisCollection[x].Text + ” <a href=”//twitter.com/”” rel=”nofollow”>” + TimeBetween + “”;
    }

    The syntax may not be entirely correct (I wrote it in notepad, not VS), but it will give you an idea of what to do. Also please take note of Ricky’s comment below that the API is getting re-written. Feel free to email me if you have any more questions.

  12. UPDATE 3/23/2010 – Ricky from Twitterizer commented below noting that basic authentication will soon go away via Twitter and OAUTH will be required. Note that the code below will only work for a few months. We will post an updated code-example soon.

    As I write this article, It’s about 75 degrees and Sunny outside. When I should be going out on a bike ride, instead I’ve opted to play with Twitterizer (an ASP.NET Twitter Library). Twitterizer is an ASP.NET library that lets you interact with the Twitter API using easy to use objects and methods. It will work with any of the .NET variants (C#, VB, J#, Windows Forms, ASP.NET, WPF, etc). I added the functionality into the 360 Web Content Management System and I thought I’d share with you how I did it.

    Here’s how to retrieve twitter feeds in ASP.NET

    (1) Get a copy of the Twitterizer Library

    First, you’ll need to get a copy of the Twitterizer Library from Google’s Codebase. The download is pretty small and contains only the application library (DLL) you need. Create a new website in ASP.NET and extract the twitterizer library to the /bin/ folder so that you can use it. Once you have it placed in your /bin/ folder, add a “using” reference to the library in the header of your page.

    using Twitterizer.Framework;

    (2) Create a “Twitter” object and Retrieve Your Status Updates.

    The library contains a few different objects that you can create. A “Twitter” object is the most generic object that you can create. Creating an instance of this object using your username and password gives you all the functionality you would normally have in Twitter, but instead of using the Twitter web interface, you’re using C# or Visual Basic. First, we’ll need to instantiate the object, and then get a collection of status updates from your account.

    Twitter thisUser = new Twitter(“UserNameHere”, “PasswordHere”);
    TwitterStatusCollection thisCollection = thisUser.Status.UserTimeline();

    (3) Loop Through Your Status Updates and Generate Some HTML

    The “TwitterStatusCollection” object type is a list of “TwitterStatus” objects, so you can use a foreach loop and go through your most recent status updates. You’ll notice in the code below that I also do some basic work with the time of the status update to generate a hyperlink to the page of the status, similar to what Twitter does.

    string TwitterCode = “”;
    foreach (TwitterStatus thisStatus in thisCollection)
    {

    TimeSpan thisSpan = new TimeSpan();
    thisSpan = DateTime.Now.Subtract(thisStatus.Created);

    string TimeBetween = “”;
    if (thisSpan.Days > 0) { TimeBetween = thisSpan.Days.ToString() + ” days ago”; }
    else if (thisSpan.Hours > 0) { TimeBetween = thisSpan.Days.ToString() + ” hours ago”;}
    else if (thisSpan.Minutes > 0) { TimeBetween = thisSpan.Days.ToString() + ” minutes ago”;}
    else if (thisSpan.Seconds > 0) { TimeBetween = thisSpan.Days.ToString() + ” seconds ago”;}

    TwitterCode += “” + thisStatus.Text + ” ” + TimeBetween + “”;
    }
    (4) Display Your Tweets

    You now have a string with your most recent twitter status updates that you can display on the page using a simple Response.Write() or you can display it in a label. You can see a variation of this code running on the “Twitter” page for the 360 Web Content Management System.

    You can also download a copy of my sample code.

  13. I downloaded the binaries from http://www.twitterizer.net/downloads/ but not sure which DLL to referece. I went for Twtiterizer2 but your code does not compile. I guess you had an older version?

  14. I got it working with this:

    UserTimelineOptions options = new UserTimelineOptions();
    options.ScreenName=”YOURUSERNAME”;

    TwitterStatusCollection recentTweets = TwitterTimeline.UserTimeline(options);

  15. I am using a JS Widget for Twitter update.. I think that working good.

    new TWTR.Widget({
    version: 2,
    type: ‘profile’,
    rpp: 5,
    interval: 6000,
    width: ‘auto’,
    height: 300,
    theme: {
    shell: {
    background: ‘#454d4a’,
    color: ‘#f0f5f0’
    },
    tweets: {
    background: ‘#ffffff’,
    color: ‘#1218e0’,
    links: ‘#4aed05’
    }
    },
    features: {
    scrollbar: true,
    loop: false,
    live: true,
    hashtags: true,
    timestamp: true,
    avatars: true,
    behavior: ‘all’
    }
    }).render().setUser(‘TugberkUgurlu’).start();

  16. this does not work with the latest realse , whats the alternative?

  17. kylepham says:

    how can i embed comments plugin twitter to asp.net?

Trackbacks

  1. […] first result in google: How to Display Your Twitter Feed using ASP.NET | Adventures In Development […]

Speak Your Mind

*