Surendra Sharma

Surendra Sharma

Search This Blog

Friday, August 23, 2013

How to get Twitter tweets on ASP.NET webpage

Now a days integration of SNS - Social Networking Sites links and feeds to our site is common requirement in almost all web application development.

This article helps how to display Twitter tweets on webpage in ASP.NET.

If you are interested into show Facebook feeds on ASP.NET page then read my article “How to show Facebook feeds on ASP.NET webpage

Twitter provides API calls to get feeds using Twitter data widget id and twitter link like https://twitter.com/YourTwitterAccountLink

Storing widget id in web.config, database is good place.

protected void Page_Load(object sender, EventArgs e)
{
TwitterID.Value = ConfigurationManager.AppSettings["TwitterID"];
}


Use below ASPX page for displaying Twitter tweets

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Show Twitter Tweets</title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $("#TwitterLink").attr("href", "https://twitter.com/YOURTWITTERNAME");
            $("#TwitterLink").attr("data-widget-id", $("#TwitterID").val());

            TwitterFeeds(document, "script", "twitter-wjs");
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <h1 align="left">
                Twitter Tweets</h1>
        </div>
        <div>
            <div>
                <div>
                    <a id="TwitterLink" border="0px" data-border-color="white">Tweets by @XYZ</a>

                    <script type="text/javascript">
                        function TwitterFeeds(d, s, id) {
                            var js, fjs = d.getElementsByTagName(s)[0];
                            if (!d.getElementById(id)) {
                                js = d.createElement(s);
                                js.id = id;
                                js.src = "//platform.twitter.com/widgets.js";
                                fjs.parentNode.insertBefore(js, fjs);
                            }
                        }
                    </script>

                </div>
            </div>
            <!--twitter//-->
        </div>
    </div>
    <asp:HiddenField ID="TwitterID" runat="server"></asp:HiddenField>
    </form>
</body>
</html>


Please leave your comments or share this tip if it’s useful for you.

No comments:

Post a Comment