Friday, September 17, 2010

How to: add 'Tweet This' button to Asp.net Webpage/website

You can add your own ‘Tweet This’ buttons to you webpage/website so that your visitors can post to twitter (complete with URL shortening using Bit.ly) is really easy.

step 1:go to solution exproler and add a webpage (i have given it name:MyTwitterPage).
step 2:remove all the HTML from the page.

step 3:Add below code in code behind file:

Imports System.Net

Partial Public Class MyTwitterPage

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Request.QueryString("url") <> "" And Request.QueryString("com") <> "" Then

TweetThis(Request.QueryString("url"), Request.QueryString("com"))

End If

End Sub

Private Sub TweetThis(ByVal sIWTFURL As String, ByVal sComment As String)

Dim sShortURL As String

Dim sFullTweet As String

sShortURL = ShortenURL(sIWTFURL)

sFullTweet = sComment & " - " & sShortURL

SentToTwitter(sFullTweet)

End Sub

Private Function ShortenURL(ByVal sURL As String) As String

Dim sJSON As String

Dim oWebClient As New WebClient

Dim sBitlyURL As String

Dim sShortURL As String

sBitlyURL = "http://api.bit.ly/shorten?version=2.0.1&longUrl=" & Server.UrlEncode(sURL) & "&login=bitlyapidemologin&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07"

sJSON = oWebClient.DownloadString(sBitlyURL)

sJSON = Mid$(sJSON, InStr(sJSON, Chr(34) & "shortUrl" & Chr(34)) + 9)

Do While sJSON.ToLower.StartsWith("http://") = False

sJSON = Mid$(sJSON, 2)

Loop

sShortURL = sJSON.Split(Chr(34))(0)

ShortenURL = sShortURL

End Function

Private Sub SentToTwitter(ByVal sComments As String)

Dim sURL As String

sURL = "http://twitter.com/home/?status=" & HttpUtility.UrlEncode(sComments)

Response.Redirect(sURL, False)

End Sub

End Class

Note: I am using the default Bit.ly login in the example, you should sign up for your own bit.ly account so you can track usage of your links.

step 4:Now you just need to add some javascript to your html pages:


< a href="#" onClick='javascript:PostToTwitter()'> Tweet This < /a >

< script >
function PostToTwitter()
{
var sTweet = 'This is the default text that will appear in the tweet';
var ShareURL = window.location.href;

window.open('http://yoursite.com/MyTwitterPage.aspx?url='+encodeURIComponent(ShareURL)+'&com='+encodeURIComponent(sTweet));
return false;
}
< /script >

Solution By:Rajesh Rolen

Share This!


45 comments:

Unknown said...

Hi..This is in Vb code..can you
please give me in C# code

Unknown said...

Hi..This Code in Vb.Net Can you please give me in c#.net.

Hardik said...

hay ravi use
http://www.developerfusion.com/tools/convert/vb-to-csharp/
to convert code

Anonymous said...

Eles aprenderam que um artista é possível que como ganhar dinheiro.

Anonymous said...

Everything iis very open with a resally clear description of the challenges.
It was truly informative. Your site iss useful.
Thank yyou for sharing!

Anonymous said...

Thiis site was... how do you say it? Relevant!! Finally I've founmd something that helped me.
Appreciate it!

Anonymous said...

Thanks very interesting blog!

Anonymous said...

Very energetic article, I loved that bit. Will there be a part 2?

Anonymous said...

WOW just what I was looking for. Came here by searching for check this

Anonymous said...

Excellent post. I used to be checking constantly this
weblog and I am inspired! Very helpful information specifically
the final part :) I deal with such info much. I was looking for this
certain information for a very lengthy time.
Thank you and good luck.

Anonymous said...

Firsdt of all I wnt to say fantastic blog! I haad a quick
question in which I'd like to ask if yyou don't mind.

I was curious to find out how you center yourself and clear your mind before writing.
I have had a difficult time clearing my thoughts in getting my thoughts out.
I truly do enjoy writing however it just seems like the first 10 to 15 minutes are usually lost simply just trying to figure out how to begin. Any recommendations or hints?
Kudos!

Anonymous said...

Ꮋi, I ԁo think this is a greаt web site. І
stumbledupon it ;) I ɑm going to come bаck once again since I book-marked it.
Money and freedom іs the ɡreatest way tߋ сhange, maay
ʏoᥙ be rich and continue to guide other people.

Anonymous said...

This is a topic that's close to my heart... Cheers! Where
are your contact details though?

Anonymous said...

Hi it's me, I am also visiting this sjte daily,
this site is really nice and the viewers are actualy sharing nice thoughts.

Anonymous said...

Do you have any video of that? I'd want to find out some additional information.

Anonymous said...

Aw, this was a very good post. Taking a few minutes and actual effort to create a
really good article… but what can I say… I hesitate a lot
and never manage to get anything done.

Anonymous said...

Great beat ! I wish to apprentice while you amend ypur website,
how could i subscribe for a blog web site? The account helped me a acceptable deal.
I had been tiny bit acquainted of this yur broadcast provided bright clear concept

Anonymous said...

Wow, tbis article is good, my younger sister is analyzing these kinds of things,
therefore I am going to inform her.

Anonymous said...

This paragraph presents clear idea in favor of the new users of blogging, that actually how to do blogging.

Anonymous said...

Hey very nice web site!! Guy .. Excellent .. Amazing ..
I will bookmark your site and take the feeds additionally?
I'm satisfied to find a lot of helpful information right here within the put
up, we want develop more strategies on this regard, thanks for sharing.
. . . . .

Anonymous said...

whoah this weblog is wonderful i like studying your posts.
Stay up the good work! You already know, a lot of people are looking around for this info, youu could help them greatly.

Anonymous said...

Hi too every , because I am truly eager of reading
this website's post to be updated daily. It carries pleasant
material.

Anonymous said...

Good day! Would youu mind if I share your blog with my facebook group?
There's a lot of folks that I think would really enjoy your content.

Please let me know. Thak you

Anonymous said...

Wow that was odd. I just wrote an very long comment but after I clicked submit my comment
didn't show up. Grrrr... well I'm not writing all that over again. Anyhow, just wanted to saay
wondeful blog!

Anonymous said...

Useful information. Lucky me I discovered your web site by chance, and I am surplrised why this accident didn't
happened earlier! I bookmarked it.

Anonymous said...

Hi there! This is kindd of off topic but I need some help from an established blog.
Is it very difficult to set up your own blog? I'm
not very techincal but I can figure things out pretty fast.
I'm thinking about creating my own but I'm nott sure where to begin. Do
you have any tips or suggestions? Thank you

Anonymous said...

Whats up are using Worddpress for yur site platform?
I'm new to the blog world butt I'm trying to get started and set up my own. Do you
require any coding expertise to make your own blog? Any help would bee
really appreciated!

Anonymous said...

Excellent pieces. Keep writing such kind of information on your site.
Im really impressed by it.
Hey there, You've performed an incredibble job.
I'll definitely digg it and individually suggest to my friends.
I am confident they will be benefited fro
this website.

Anonymous said...

Heya i'm for the primary time here. I came across ths board
and I find It truly useful & it hekped me out a lot.
I hope to give one thing back andd aid others such as you aided me.

Anonymous said...

You are so interesting! I don't suppose I've truly read anything lkke this before.

So good too discover somebody with unique thoughts onn this issue.
Really.. thank you for stqrting thi up. This site iss something that is needed on the web, someone with soe originality!

Anonymous said...

It's perfect time to make some planbs for the future and it iss time to be happy.
I've read this post and if I could I wish to suggest you few
interesting things orr advice. Maybe you
can write next articles referring tto this article.
I desore to read even more things about it!

Anonymous said...

Hi there! I know this is kinda off topoic but I
waas wondering which blog platform are you using for this site?
I'm getting fed upp of Wordpress because I've had
issues with hackers and I'm looking at options for another platform.
I would be awesome iif you could point me in thee direction of a good
platform.

Anonymous said...

Hello, I think you website might be having browser compatibility issues.
When I look at your blog site in Safari, it looks
fine but when opening in Internet Explorer, it has
some overlapping. I just wanted to give you a quick heads up!
Othdr then that, amazing blog!

Anonymous said...

For the reason that the admin of this site is working, no doubnt very shortly it will be
famous, due tto its feature contents.

Anonymous said...

Nice post. I learn something totally new and challenging on sites I stumbeupon everyday.
It will always be interesting to ead articles from other authirs and practice
soomething from their websites.

Anonymous said...

I do not know if it's just me or if everybody else experiencing problems with your website.
It llooks like somee of the written text in your conent are running off the screen. Can somebody else please provide
feedback and let me know if this is happening to them too?

This could be a issue with my internet browser because
I've had this happen before. Many thanks

Anonymous said...

For latest information you have to visit internet and on the web I fouund this website as a finest site
for newest updates.

Anonymous said...

hello!,I really like your writing very so much! share
we be in contact extra approximately your arfticle on AOL?
I require a specialist in this area to solve my problem. May
be that is you! Having a look forward to peer you.

Anonymous said...

Attractive component to content. I simply stumbled upon youhr
website and in accession capital to assedrt that I gett in fact enjoyed account
your weblog posts. Any way I'll be subscribing in your augment or even I fulfillment you access persistently rapidly.

Anonymous said...

Hi therre this is kiond of of off topic but I was wanting to know iff blogs
use WYSIWYG edutors or if yoou have to manually code with HTML.
I'm starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience.
Any help would be greatly appreciated!

Anonymous said...

I don'tknowif it's just me oor if perhaps everybody
else encountering poblems with yoiur website. It looks like some of the text on your pozts are running off
the screen. Can somebody else plesse provide feedback and let me know if this is happening to
them as well? This may be a issue with my internet
brosser becaue I've had this happen previously.
Kudos

Anonymous said...

Everything is very open with a precise clarificfation of the challenges.

It was really informative. Your site is extremely
helpful. Many thanks for sharing!

Anonymous said...

This is thee right website for everyone who wants to find out about thiks
topic. You know so much its almost hard to aargue with you (not that I actually
will need to…HaHa). You definitely put a brand new spin on a subject that's been written about for years.
Wonderful stuff, just excellent!

Anonymous said...

What's up Dear, are you really visiting this site on a regular basis, if so then you
will definitely get nice knowledge.

Anonymous said...

How to get rid of my yellow-colored fever of Korean males?!?
It began with kpop and now I feel like Korean boys will be the only men I find incredibly hot.
I obsess over them all the time and get thrilled when I
see an Asian. I feel so weird and disgusted with myself as I'm not
Korean myself. In any case to avoid this? Get a Korean BF ...

Powered By Blogger · Designed By Seo Blogger Templates