Click here to Skip to main content
Click here to Skip to main content

Displaying Twitter (tweets) in ASP.NET

By , 9 Dec 2010
Rate this:
Please Sign up or sign in to vote.

Introduction

Hi everyone!

Again this article is the fruit of a need I had. I've been searching for a couple of days how to show social networks' updates (from Twitter, YouTube, Facebook...) and after getting a terrible headache, I decided to do it myself. And I want to share how I did it.

Background

The truth is that all these Social Networks have APIs that allow developers to integrate our applications with them, and they are great. There are also open source libraries that we could use (like "twitterizer"), and it works pretty well also. But the reality is that they have too many options and documentation to read... and (for me at least), it was not worth it to spend that time when my requirement was as simple as showing my latest tweets and videos in the company web page. I found a guy wondering in a forum how he couldn't find a simple example on how to do this same thing with these open source libraries or with the APIs, though I insist, to integrate whole applications, libraries and APIs are great!

What We'll Do

We'll see Twitter this time, and we'll use Twitter's feed to get a user's timeline (the tweets he or she sent). Twitter returns the information in several formats (rss, xml, json...) and we'll talk about XML.

Using the Code

The variable "twitterUrl" that will be used in the code must be something like this:

twitterUrl = "http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=nereolopez"

".xml" can be replaced by the desired format, and screen name would be the twitter screen name that we want to retrieve, in this example, my own.

Private Sub getTwitterInfo()        
        Dim str As String
        Dim client As New WebClient
        str = client.DownloadString(twitterUrl)

        Dim myXml As New XmlDocument
        myXml.LoadXml(str)

        Dim ds As New DataSet
        ds.ReadXml(New XmlNodeReader(myXml))

        Dim dt As New DataTable()
        dt.Columns.Add("Titulo")
        dt.Columns.Add("Fecha")
        dt.Columns.Add("Link")

        Dim row As DataRow
        If ds.Tables("status") IsNot Nothing AndAlso ds.Tables(0).Rows.Count > 0 Then
            For i As Integer = 0 To ds.Tables("status").Rows.Count - 1
                row = dt.NewRow()
                row("Titulo") = ds.Tables("status").Rows(i)(2).ToString()
                row("Fecha") = ds.Tables("status").Rows(i)(0).ToString()
                Dim lnk As String = "http://twitter.com/" & _
		ds.Tables("user").Rows(i)(2).ToString() & "/statuses/"
                row("Link") = lnk & ds.Tables("status").Rows(i)(1).ToString()
                dt.Rows.Add(row)
            Next
        End If

        dlTwitter.DataSource = dt
        dlTwitter.DataBind()
        dt.Dispose()
    End Sub

twitterResult.jpg

First, we create a WebClient to make a request. We pass the URL with the user we want to retrieve and the format.

We create an XmlDocument and read the string to fill that document.

We create a datatable with three columns: Title, Date and Link.

When we read our XML document into a dataset, we get three different tables: statuses, status and user.

All the information that we want to display is in "status" table. We retrieve the date(position 0), the guid(position 1, with this we'll construct the link to the tweet itself), tweet (position 2), we could also use the source (position 3) but it's enough for this example. We'll also use the table "user" to get the user name to create the link. As we're iterating through the dataset's tables, we extract the information we need to the datatable we created.

I have a DataList in the aspx. You can create the layout you want. You can see the result of mine in the picture above. In these lines of code, we bind the DataTable we created to the DataList.

Points of Interest

I notice some differences between the strings that we obtain through XML and RSS format. Apart from having a different schema, XML does not show the retweets in the timeline of that user. Another important thing is that we are not going to receive any information if the user's tweets are protected (private).

Doing this with YouTube is similar. I will write an article on it in a couple days.

Thanks for reading and please feel free to make comments, suggestions, questions or whatever you want.

Hope this helps!

History

  • 9th December, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

nereo.lopez
Software Developer (Senior)
Spain Spain
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 3 PinmemberDan Colasanti27-Jan-11 8:57 
Generalnice one PinmemberPranay Rana14-Dec-10 21:09 
QuestionWhat about this? Pinmemberprerakpatel9-Dec-10 20:36 
AnswerRe: What about this? Pinmembernereo.lopez10-Dec-10 4:46 
AnswerRe: What about this? Pinmemberjgasm13-Dec-10 15:19 
GeneralRe: What about this? Pinmemberac_1318-Jan-11 1:54 
GeneralWhy an image PinmemberDaveAuld9-Dec-10 5:51 
GeneralRe: Why an image PinsubeditorIndivara9-Dec-10 15:49 
AnswerRe: Why an image Pinmembernereo.lopez9-Dec-10 20:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 10 Dec 2010
Article Copyright 2010 by nereo.lopez
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid