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

Displaying Twitter (tweets) in ASP.NET

By , 9 Dec 2010
 

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhat about this?memberprerakpatel9 Dec '10 - 20:36 
Just adding a div can get you the recent tweets.
http://www.codeproject.com/Tips/135057/Recent-tweets.aspx
[^]
AnswerRe: What about this? Pinmembernereo.lopez10 Dec '10 - 4:46 
Yeap, that's nice. There're also several widgets that twitter offers also... If simple, I like custom things.
AnswerRe: What about this? Pinmemberjgasm13 Dec '10 - 15:19 
Well I agree you can do this similarly with a DIV tag, unfortunately, you do not get the same options to interact with the data in code.
GeneralRe: What about this? Pinmemberac_1318 Jan '11 - 1:54 

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

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