Matt Paulson

Entrepreneur, Author, Private Equity Investor
  • Facebook
  • Linkedin
  • Twitter
  • Rss
  • Blog
    • Book Recommendations
    • Church Life & Ministry
    • Community Projects
    • Entrepreneurship
    • Quaterly Updates
    • Startup Community
    • Startup Q&A Show
    • Web Development
  • About
    • Resume
  • My Companies
    • Angel Investments
  • My Books
    • Online Business from Scratch
    • Automatic Income
    • The Ten-Year Turnaround
    • Email Marketing Demystified
    • 40 Rules for Internet Business Success
    • Business Growth Day by Day
    • Simple Savings
  • Media Appearances
  • Contact
Home» Web Development » How to Display Your Twitter Feed using ASP.NET

How to Display Your Twitter Feed using ASP.NET

Posted on June 21, 2009 by matt in Web Development 13 Comments
Tweet

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.

Tweet

Comments

13 comments on “How to Display Your Twitter Feed using ASP.NET”

  1. Mushtaq says:
    July 10, 2009 at 2:44 am

    execellent , it was up to my needs..

    Thanks for the post

    Reply
  2. L Heyns says:
    August 11, 2009 at 2:11 pm

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

    Reply
  3. Adam Brown says:
    August 12, 2009 at 8:29 am

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

    Great post

    Reply
  4. vijayamalla says:
    October 27, 2009 at 9:57 am

    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?

    Reply
  5. Jeremy says:
    October 27, 2009 at 10:50 am

    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

    Reply
  6. David says:
    February 8, 2010 at 3:26 pm

    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.

    Reply
  7. Kartikeya says:
    February 9, 2010 at 3:14 am

    I am using Twitterizer and my code is as follow:

    string uname = "[email protected]";
    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?

    Reply
  8. Ricky says:
    March 23, 2010 at 8:51 am

    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.

    Reply
    • Matthew Paulson says:
      March 23, 2010 at 9:47 am

      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.

      Reply
  9. brian says:
    March 24, 2010 at 7:33 am

    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

    Reply
  10. Matthew Paulson says:
    March 24, 2010 at 10:16 am

    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<TweetLimit; x++)
    {

    TimeSpan thisSpan = new TimeSpan();
    thisSpan = DateTime.Now.Subtract(thisCollection[x].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 += ” + thisCollection[x].Text + ” ” + 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.

    Reply
  11. wsf wsds says:
    September 10, 2010 at 5:49 am

    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.

    Reply
  12. Fahad Jadun says:
    August 7, 2014 at 9:04 pm

    it gives error 404 after compiling

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Email Updates

Enter your email address below to receive a steady stream of tricks, tips and ideas to help you build a better and more profitable business.

Email Marketing Demystified

The second edition of Email Marketing Demystified is now available. This book teaches you how to build a massive email list, write marketing copy that converts and generate more sales in your business.

Click Here to Get Your Copy of Email Marketing Demystified

Recent Posts

  • Leadership Sioux Falls is Fundraising for New Playground Equipment at Hayward Park. Here’s How You Can Help.

    March 2, 2021
  • Sioux Falls to Celebrate Entrepreneurship Day on March 3, 2021

    February 18, 2021
  • MarketBeat is Hiring for Three Positions (Support, Data Analyst and Web Developer)

    February 17, 2021
  • The Biggest Mistake You Can Make with Your Email Sign-Up Forms

    February 16, 2021
  • Five Lessons Learned from the MarketBeat Burger Project

    February 2, 2021

    Follow me on Facebook & YouTube

    Contact

    • [email protected]
    • Contact Us
    • matthew-paulson
      • Facebook
      • Twitter
      • Linkedin
      • Instagram
      • Rss

    © Matthew Paulson 2003-2021. All Rights Reserved.

    Privacy Policy