"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Twitter Helper with ASP.NET Web Pages

by Tom FitzMacken

This topic and application show how to add a Twitter Helper to your WebMatrix 3 project. It contains the Twitter Helper code and shows how to call the helper methods.

This code for the Twitter.cshtml file was developed by Tian Pan of Microsoft.

Software versions used in the tutorial

  • ASP.NET Web Pages (Razor) 3

This tutorial also works with ASP.NET Web Pages 2.

Introduction

This topic demonstrates how to add a Twitter Helper to your application and use Razor syntax to call the helper methods. The Twitter Helper makes it easy to incorporate Twitter buttons and widgets in your application. To use a Twitter widget, such as a user’s timeline or the search results for a hashtag, you must first create the widget on Twitter. After creating your widget, you will receive a widget id. You pass this widget id as a parameter when calling the helper methods that show widget.

This topic was written for version 1.1 of the Twitter API. By directly adding the Twitter Helper code to your project, you can update the helper code if the Twitter API changes.

For information about installing WebMatrix, see Introducing ASP.NET Web Pages 2 - Getting Started.

Add Twitter Helper to your project

To add the Twitter Helper, first, add a folder named App_Code to your project. Then, create a file named Twitter.cshtml.

App_Code folder
App_Code folder

Replace the default code in Twitter.cshtml with the following code.

[!code-cshtmlMain]

   1:  @* This Twitter helper is compatible with version 1.1 of the Twitter API. *@
   2:   
   3:  @using System.Globalization
   4:   
   5:  @* For more about the twitter follow button, please visit
   6:     https://dev.twitter.com/docs/follow-button *@
   7:  @helper FollowButton(
   8:              string userName,
   9:              bool showCount = false,
  10:              bool showUserName = true,
  11:              bool largeButton = false,
  12:              bool optOutOfTailoring = false,
  13:              string language = "",
  14:              bool alignLeft = true)
  15:  {
  16:      var showCountAttribute = new HtmlString(showCount ? "" : "data-show-count=\"false\"");
  17:      var showUserNameAttribute = new HtmlString(showUserName ? "" : "data-show-screen-name=\"false\"");
  18:      var largeButtonAttribute = new HtmlString(largeButton ? "data-size=\"large\"" : "");
  19:      var optOutOfTailoringAttribute = new HtmlString(optOutOfTailoring ? "data-dnt=\"true\"" : "");
  20:      var languageAttribute = new HtmlString(!language.IsEmpty() && !language.Equals("en", StringComparison.OrdinalIgnoreCase) ? String.Format(CultureInfo.InvariantCulture, " data-lang=\"" + HttpUtility.HtmlAttributeEncode(language) + "\"") : "");
  21:      var alignAttribute = new HtmlString(alignLeft ? "" : "data-align=\"right\"");
  22:      <a href="https://twitter.com/@HttpUtility.UrlEncode(userName)" class="twitter-follow-button" @showCountAttribute @showUserNameAttribute @largeButtonAttribute @optOutOfTailoringAttribute @languageAttribute @alignAttribute>Follow @@@userName)</a>
  23:      <script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + '://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js, fjs); } }(document, 'script', 'twitter-wjs');</script>
  24:  }
  25:   
  26:  @* For more about the tweet button, please visit https://dev.twitter.com/docs/tweet-button *@
  27:  @helper TweetButton(string url = "",
  28:              string tweetText = "",
  29:              bool showCount = true,
  30:              string via = "",
  31:              string recommend = "",
  32:              string hashtag = "",
  33:              bool largeButton = false,
  34:              bool optOutOfTailoring = false,
  35:              string language = "")
  36:  {
  37:      var urlAttribute = new HtmlString(url.IsEmpty() ? "" : String.Format(CultureInfo.InvariantCulture, " data-url=\"" + HttpUtility.HtmlAttributeEncode(url) + "\""));
  38:      var tweetTextAttribute = new HtmlString(tweetText.IsEmpty() ? "" : "data-text=\"" + HttpUtility.HtmlAttributeEncode(tweetText) + "\"");
  39:      var showCountAttribute = new HtmlString(showCount ? "" : "data-show-count=\"false\"");
  40:      var viaAttribute = new HtmlString(via.IsEmpty() ? "" : "data-via=\"" + HttpUtility.HtmlAttributeEncode(via) + "\"");
  41:      var recommendAttribute = new HtmlString(recommend.IsEmpty() ? "" : "data-related=\"" + HttpUtility.HtmlAttributeEncode(recommend) + "\"");
  42:      var hashtagAttribute = new HtmlString(hashtag.IsEmpty() ? "" : "data-hashtags=\"" + HttpUtility.HtmlAttributeEncode(hashtag) + "\"");
  43:      var largeButtonAttribute = new HtmlString(largeButton ? "data-size=\"large\"" : "");
  44:      var optOutOfTailoringAttribute = new HtmlString(optOutOfTailoring ? "data-dnt=\"true\"" : "");
  45:      var languageAttribute = new HtmlString(!language.IsEmpty() && !language.Equals("en", StringComparison.OrdinalIgnoreCase) ? String.Format(CultureInfo.InvariantCulture, " data-lang=\"{0}\"", HttpUtility.HtmlAttributeEncode(language)) : "");
  46:      <a href="https://twitter.com/share" class="twitter-share-button" @urlAttribute @tweetTextAttribute @showCountAttribute @viaAttribute @recommendAttribute @hashtagAttribute @largeButtonAttribute @optOutOfTailoringAttribute @languageAttribute>Tweet</a>
  47:      <script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + '://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js, fjs); } }(document, 'script', 'twitter-wjs');</script>
  48:  }
  49:   
  50:  @helper TimeLine(string userName,
  51:              string widgetId,
  52:              string theme = "",
  53:              string linkColor = "",
  54:              string language = "",
  55:              int? tweetLimit = null,
  56:              string relatedUsers = "",
  57:              string ariaPolite = "",
  58:              int? width = null,
  59:              int? height = null,
  60:              string title = "Tweets")
  61:  {
  62:      var themeAttribute = new HtmlString(theme.IsEmpty() ? "" : "data-theme=\"" + theme + "\"");
  63:      var linkColorAttribute = new HtmlString(linkColor.IsEmpty() ? "" : "data-link-color=\"" + linkColor + "\"");
  64:      var languageAttribute = new HtmlString(!language.IsEmpty() && !language.Equals("en", StringComparison.OrdinalIgnoreCase) ? String.Format(CultureInfo.InvariantCulture, " data-lang=\"{0}\"", HttpUtility.HtmlAttributeEncode(language)) : "");
  65:      var relatedUsersAttribute = new HtmlString(relatedUsers.IsEmpty() ? "" : "data-related=\"" + relatedUsers + "\"");
  66:      var ariaPoliteAttribute = new HtmlString(ariaPolite.IsEmpty() ? "" : "data-aria-polite=\"" + ariaPolite + "\"");
  67:      <a class="twitter-timeline" href="https://twitter.com/@userName" width="@width.ToString()" height="@height.ToString()" data-widget-id="@HttpUtility.HtmlEncode(widgetId)" @themeAttribute @linkColorAttribute @languageAttribute data-tweet-limit="@tweetLimit.ToString()" @relatedUsersAttribute @ariaPoliteAttribute>@title</a>
  68:      <script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + "://platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); } }(document, "script", "twitter-wjs");</script>
  69:  }
  70:   
  71:  @helper Profile(string userName,
  72:              string widgetId,
  73:              string theme = "",
  74:              string linkColor = "",
  75:              string language = "",
  76:              int? tweetLimit = null,
  77:              string relatedUsers = "",
  78:              string ariaPolite = "",
  79:              int? width = null,
  80:              int? height = null)
  81:  {
  82:      @Twitter.TimeLine(userName, widgetId, theme, linkColor, language, tweetLimit, relatedUsers, ariaPolite, width, height, "Tweets by @" + HttpUtility.HtmlEncode(userName));
  83:  }
  84:   
  85:  @helper Faves(string userName,
  86:              string widgetId,
  87:              string theme = "",
  88:              string linkColor = "",
  89:              string language = "",
  90:              int? tweetLimit = null,
  91:              string relatedUsers = "",
  92:              string ariaPolite = "",
  93:              int? width = null,
  94:              int? height = null)
  95:  {
  96:      @Twitter.TimeLine(userName + "/favorites", widgetId, theme, linkColor, language, tweetLimit, relatedUsers, ariaPolite, width, height, "Favorite Tweets by @" + HttpUtility.HtmlEncode(userName));
  97:  }
  98:   
  99:  @helper List(string userName,
 100:              string list,
 101:              string widgetId,
 102:              string theme = "",
 103:              string linkColor = "",
 104:              string language = "",
 105:              int? tweetLimit = null,
 106:              string relatedUsers = "",
 107:              string ariaPolite = "",
 108:              int? width = null,
 109:              int? height = null)
 110:  {
 111:      @Twitter.TimeLine(userName + "/" + list, widgetId, theme, linkColor, language, tweetLimit, relatedUsers, ariaPolite, width, height, "Tweets from @" + HttpUtility.HtmlEncode(userName) + "/" + HttpUtility.HtmlEncode(list));
 112:  }
 113:   
 114:  @helper Search(string query,
 115:              string widgetId,
 116:              string theme = "",
 117:              string linkColor = "",
 118:              string language = "",
 119:              int? tweetLimit = null,
 120:              string relatedUsers = "",
 121:              string ariaPolite = "",
 122:              int? width = null,
 123:              int? height = null)
 124:  {
 125:      @Twitter.TimeLine("search?q=" + HttpUtility.UrlEncode(query), widgetId, theme, linkColor, language, tweetLimit, relatedUsers, ariaPolite, width, height, "Tweets about \"" + HttpUtility.HtmlEncode(query) + "\"");
 126:  }

Call Twitter methods from your web pages

The following example shows how to use the Twitter Helper methods from a page in your project. In your project, you will want to replace the parameter values with values that are relevant to your needs. You can use the provided widget ids to explore how the methods work, but you will want to generate your own widgets for your project.

Not all of the parameters shown below are required. The optional parameters are used to customize how the button or widget is displayed. For example, the Follow Button only requires the user name to follow, but the example shows how to include the number of followers, and how specify the size of the button and the language.

[!code-htmlMain]

   1:  <!DOCTYPE html>
   2:   
   3:  <html lang="en">
   4:      <head>
   5:          <meta charset="utf-8" />
   6:          <title>Show the Twitter Helper</title>
   7:      </head>
   8:      <body>
   9:          <h1>Twitter Buttons and Widgets</h1>
  10:   
  11:          <h2>1. Follow Button</h2>
  12:          <p>@Twitter.FollowButton(userName: "aspnet", showCount: true, largeButton: false, language: "es")</p>
  13:   
  14:          <h2>2. Tweet Button</h2>
  15:          <p>@Twitter.TweetButton(url: "http://www.asp.net/mvc", largeButton: true, hashtag: "awesome")</p>
  16:          
  17:          <h2>3. User Timeline (Profile)</h2>
  18:          <p>@Twitter.Profile("aspnet", "370310677673422848")</p>
  19:   
  20:          <h2>4. Favorites</h2>
  21:          <p>@Twitter.Faves("Microsoft", "372486349795753984")</p>
  22:   
  23:          <h2>5. List</h2>
  24:          <p>@Twitter.List("MS_Consumer_Bands", "Microsoft", "372485258974748672")</p>
  25:   
  26:          <h2>6. Search</h2>
  27:          <p>@Twitter.Search("#asp.net", "370310477957435392")</p>
  28:      </body>
  29:  </html>

See the results

The above code produces the following buttons and widgets. These buttons and widgets are fully-functional, not screenshots. The Follow Button is displayed in Spanish because the language parameter was set to es.

Follow Button

[Follow @aspnet)](https://twitter.com/aspnet)

Tweet Button

Tweet

User Timeline (Profile)

[Tweets by @aspnet](https://twitter.com/aspnet)

Favorites

[Favorite Tweets by @Microsoft](https://twitter.com/Microsoft/favorites)

List

[Tweets from @Microsoft/MS\_Consumer\_Bands](https://twitter.com/microsoft/ms-consumer-brands/) Tweets about “#asp.net”

Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-pages/overview/ui-layouts-and-themes/twitter-helper.htm
< THANKS ME>