In a previous post, I showcased a “proof of concept” for shortcodes in Sitecore — this is a shorthand notation for embedding things like YouTube videos in your webpages without having to type up a bunch of HTML — and felt I should follow up with another “proof of concept” around incorporating Embedded Tweets in Sitecore.
You might be asking “what’s an Embedded Tweet?” An Embedded Tweet is basically the process of pasting a Tweet URL from Twitter into an editable content area of your website/blog/whatever (think Rich Text field in Sitecore), and let the code that builds the HTML for your site figure out how to display it.
For example, I had used an Embedded Tweet in a recent post:
This is what is seen on the rendered page:
While doing some research via Google on how to do this in Sitecore, I found this page from Twitter that discusses how you could go about accomplishing this, and discovered how to get JSON containing information about a Tweet — including its HTML — using one of Twitter’s API URLs:
The JSON above drove me to build the following POCO class to represent data returned by that URL:
using System.Runtime.Serialization; namespace Sitecore.Sandbox.Pipelines.RenderField.Tweets { public class Tweet { [DataMember(Name = "cache_age")] public int CacheAgeMilliseconds { get; set; } [DataMember(Name = "url")] public string Url { get; set; } [DataMember(Name = "html")] public string Html { get; set; } } }
I decided to omit some of the JSON properties returned by the Twitter URL from my class above — width and height are examples — since I felt I did not need to use them for this “proof of concept”.
I then leveraged the class above in the following class that will serve as a <renderField> pipeline processor to embed Tweets:
using System; using System.IO; using System.Net; using System.Text.RegularExpressions; using System.Web; using Sitecore.Caching; using Sitecore.Diagnostics; using Sitecore.Pipelines.RenderField; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace Sitecore.Sandbox.Pipelines.RenderField.Tweets { public class ExpandTweets { private string TwitterWidgetScriptTag {get ; set; } private string TwitterApiUrlFormat { get; set; } private string _TweetPattern; private string TweetPattern { get { return _TweetPattern; } set { _TweetPattern = value; if (!string.IsNullOrWhiteSpace(_TweetPattern)) { _TweetPattern = HttpUtility.HtmlDecode(_TweetPattern); } } } private HtmlCache _HtmlCache; private HtmlCache HtmlCache { get { if (_HtmlCache == null) { _HtmlCache = CacheManager.GetHtmlCache(Context.Site); } return _HtmlCache; } } public void Process(RenderFieldArgs args) { Assert.ArgumentNotNull(args, "args"); AssertRequired(); if(!ShouldFieldBeProcessed(args)) { return; } args.Result.FirstPart = ExpandTweetUrls(args.Result.FirstPart); } private static bool ShouldFieldBeProcessed(RenderFieldArgs args) { Assert.ArgumentNotNull(args, "args"); Assert.ArgumentNotNull(args.FieldTypeKey, "args.FieldTypeKey"); string fieldTypeKey = args.FieldTypeKey.ToLower(); return fieldTypeKey == "text" || fieldTypeKey == "rich text" || fieldTypeKey == "single-line text" || fieldTypeKey == "multi-line text"; } private void AssertRequired() { Assert.IsNotNullOrEmpty(TwitterWidgetScriptTag, "TwitterWidgetScriptTag must be set! Check your configuration!"); Assert.IsNotNullOrEmpty(TwitterApiUrlFormat, "TwitterApiUrlFormat must be set! Check your configuration!"); Assert.IsNotNullOrEmpty(TweetPattern, "TweetPattern must be set! Check your configuration!"); } protected virtual string ExpandTweetUrls(string html) { string htmlExpanded = html; MatchCollection matches = Regex.Matches(htmlExpanded, TweetPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); foreach (Match match in matches) { string tweetHtml = GetTweetHtml(match.Groups["id"].Value); if (!string.IsNullOrWhiteSpace(tweetHtml)) { htmlExpanded = htmlExpanded.Replace(match.Value, tweetHtml); } } if (matches.Count > 0) { htmlExpanded = string.Concat(htmlExpanded, TwitterWidgetScriptTag); } return htmlExpanded; } protected virtual string GetTweetHtml(string id) { string html = GetTweetHtmlFromCache(id); if (!string.IsNullOrWhiteSpace(html)) { return html; } Tweet tweet = GetTweetFromApi(id); AddTweetHtmlToCache(id, tweet); return tweet.Html; } private string GetTweetHtmlFromCache(string id) { return HtmlCache.GetHtml(id); } private void AddTweetHtmlToCache(string id, Tweet tweet) { if (string.IsNullOrWhiteSpace(tweet.Html)) { return; } if (tweet.CacheAgeMilliseconds > 0) { HtmlCache.SetHtml(id, tweet.Html, DateTime.Now.AddMilliseconds(tweet.CacheAgeMilliseconds)); return; } HtmlCache.SetHtml(id, tweet.Html); } protected virtual Tweet GetTweetFromApi(string id) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(TwitterApiUrlFormat, id)); try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { var result = reader.ReadToEnd(); JObject jObject = JObject.Parse(result); return JsonConvert.DeserializeObject<Tweet>(jObject.ToString()); } } catch (Exception ex) { Log.Error(this.ToString(), ex, this); } return new Tweet { Html = string.Empty }; } } }
Methods in the class above find all Tweet URLs in the Rich Text, Single-Line Text, or Multi-Line Text field being processed — the code determines if it’s a Tweet URL based on a pattern that is supplied by a configuration setting (you will see this below in this post); extract Tweets’ Twitter identifiers (these are located at the end of the Tweet URLs); and attempt to find the Tweets’ HTML in Sitecore’s HTML cache.
If the HTML is found in cache for a Tweet, we return it. Otherwise, we make a request to Twitter’s API to get it, put it in cache one we have it (it is set to expire after a specified number of milliseconds from the time it was retrieved: Twitter returns the number of milliseconds in one full year by default), and then we return it.
If the returned HTML is not empty, we replace it in the field’s value for display.
If the HTML returned is empty — this could happen when an exception is encountered during the Twitter API call (of course we log the exception in the Sitecore log when this happens 😉 ) — we don’t touch the Tweet URL in the field’s value.
Once all Tweet URLs have been processed, we append a script tag referencing Twitter’s widget.js file — this is supplied through a configuration setting, and it does the heavy lifting on making the Tweet HTML look Twitterific 😉 — to the field’s rendered HTML.
I then tied everything together using the following patch configuration file:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <pipelines> <renderField> <processor type="Sitecore.Sandbox.Pipelines.RenderField.Tweets.ExpandTweets, Sitecore.Sandbox" patch:after="processor[@type='Sitecore.Pipelines.RenderField.GetTextFieldValue, Sitecore.Kernel']"> <TwitterWidgetScriptTag><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script></TwitterWidgetScriptTag> <TwitterApiUrlFormat>https://api.twitter.com/1/statuses/oembed.json?id={0}&omit_script=true</TwitterApiUrlFormat> <TweetPattern>https://twitter.com/.+/status/(?<id>\d*)</TweetPattern> </processor> </renderField> </pipelines> </sitecore> </configuration>
Let’s see this in action!
I created a test Item, and added some legitimate and bogus Tweet URLs into one of its Rich Text fields (please pardon the grammatical issues in the following screenshots :-/):
This isn’t the most aesthetically pleasing HTML, but it will serve its purpose for testing:
After saving and publishing, I navigated to my test Item’s page, and saw this:
If you have any suggestions on making this better, or have other ideas for embedding Tweets in Sitecore, please share in a comment.
Nice! I did this using TweetSharp and adding a form within sitecore that contained all the security info. Then I created a content item with the number of tweets to show and the twitter account to display. I just I ended up caching it using just a regular asp.net cache and expiring that after an hour. Mine just grabs the top X number of tweets and displays them. TweetSharp really makes it easy to handle all the twitter communication and display the results.
I recently released a Sitecore Rocks Nuget package that allows you to download Facebook posts and Tweets and store them within Sitecore as items. Then you can style it however you see fit. Check it out https://www.nuget.org/packages/Sitecore.SocialAggregator/