Twitter continues to gain in popularity, in part because it offers a rich and easy to use query API.
Just for fun I made an ASP.NET Web Forms User Control to Display some recent Tweets on a web page.
The tweets are displayed in a ListView as follows.
- <%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”MyTweets.ascx.cs” Inherits=”NETOOP.Controls.MyTweets” %>
- <%@ OutputCache Duration=”3600″ VaryByParam=”None” %>
- <asp:ListView ID=”MyTweetsListView” runat=”server”>
- <LayoutTemplate>
- <table class=”gridview”>
- <th>My Tweets</th>
- <tbody>
- <asp:PlaceHolder ID=”itemPlaceholder” runat=”server” />
- </tbody>
- </table>
- </LayoutTemplate>
- <ItemTemplate>
- <tr>
- <td><a href=’<%#Eval(“Url”)%>‘><%#Eval(“Title”) %></a></td>
- </tr>
- </ItemTemplate>
- </asp:ListView>
Note the use of the OutputCache directive so the Twitter API doesn’t need to get called for very single request of any page that contains this control.
Since Microsoft’s .NET is so XML savvy and LINQ to XML is so powerful the C# code to fetch the tweets and bind to the ListView is pretty straight forward.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Xml.Linq;
- namespace NETOOP.Controls
- {
- public partial class MyTweets : System.Web.UI.UserControl
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- XNamespace slashNamespace = “http://purl.org/rss/1.0/modules/slash/”;
- XDocument rssFeed = XDocument.Load(“http://www.twitter.com/statuses/user_timeline/misfitgeek.rss” + “?count=5″);
- var posts = from item in rssFeed.Descendants(“item”)
- select new
- {
- Title = item.Element(“title”).Value,
- Url = item.Element(“link”).Value,
- };
- MyTweetsListView.DataSource = posts;
- MyTweetsListView.DataBind();
- }
- }
- }
Notice that he URL in the XDocument.Load specifies “misfitgeek.rss” to pull my personal tweets (which you can include on your web site if you like). You could change the portion of the URL to reflect your own Twitter name.






















RE: Add your recent Tweets to your web site.
Pingback from Twitter Trackbacks for Add your recent Tweets to your web site. : Misfit Geek [msjoe.com] on Topsy.com
RE: Add your recent Tweets to your web site.
Pingback from Add your recent Tweets to your web site. : Misfit Geek
Great article, question for you (forgive my ignorance): what does the XNamespace slashNamespace do? I don’t see it referenced.
RE: Add your recent Tweets to your web site.
Thank you for submitting this cool story – Trackback from DotNetShoutout
Great article man, what is the role of slashNamespace?? and where did u get this link from??
Ahh, sorry guys. The slashNamespace variable should have been deleted. I had been playing arounf with Perminant Uniform Resource Locators (see Purl.com)
one more great article…