Twitter Search WebPart

I used to play in a punk rock band, and we often joked that with only so many bar chords to play, we often copied our own songs unintentionally. When you are writing a blog, it can be difficult to know when someone else has already written on the subject you’ve chosen, or if you’ve just coincidentally stumbled upon something already out there. I am positive there are other Twitter-related SharePoint posts written, but I haven’t seen any offer a configurable webpart like I’m about to do. If it’s been done before, I still consider the time I spent valuable, because this was a fun item to tackle.

Finished Product Screenshots


Here are the inspirations for my post, including some of the source code for querying the Twitter Search API via Linq

Here is how I made this project my own

  1. Created a new SharePoint solution that contains a site-scoped feature for adding a Twitter Search WebPart to your site
  2. WebPart has the main Twitter search API query abilities:
    1. General search term
    2. From user
    3. To user
    4. About user
    5. Hashtag
  3. Added the ability to define a QueryString variable that overrides the webpart configuration and takes the search directly from the QueryString
  4. Used caching in combination with Since_Id to cut back on amount of data being brought back with each request
  5. Enhanced the Linq query to bring back the profile images (or substitute Twitter’s default image when one is not present)
  6. Added the ability to turn profile images on and off in results display
  7. Used regular expressions to hyperlink usernames and urls embedded in status updates (text search returns has html stripped)
  8. There are no SharePoint references in the webpart, and it inherits from the regular WebPart class, so it could easily be used in an ASP.Net 3.5 Application

Here’s the WebPart code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
namespace MattJimison.Twitter
{
    [Guid("d7bd616b-fa72-4299-a1c1-4c2d79f1970d")]
    public class TwitterSearchWebPart : WebPart
    {
 
        #region Properties / Fields
        private bool m_DisplayImage = true;
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("Display Profile Image")]
        [WebDescription("Display Profile Image")]
        public bool DisplayImage
        {
            get { return m_DisplayImage; }
            set { m_DisplayImage = value; }
        }
 
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("Search Text")]
        [WebDescription("Search Text")]
        public string SearchText { get; set; }
 
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("From")]
        [WebDescription("From User")]
        public string From { get; set; }
 
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("To")]
        [WebDescription("To User")]
        public string To { get; set; }
 
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("About")]
        [WebDescription("About User")]
        public string About { get; set; }
 
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("Hashtag")]
        [WebDescription("Hashtag")]
        public string HashTag { get; set; }
 
        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [Category("Twitter Search")]
        [WebDisplayName("QueryString Variable")]
        [WebDescription("QueryString Variable")]
        public string QueryStringVariable { get; set; }
 
        public List<Tweet> CachedTweets
        {
            get
            {
                if (HttpContext.Current.Cache[m_Query] != null)
                {
                    return (List<Tweet>)HttpContext.Current.Cache[m_Query];
                }
                else
                {
                    return new List<Tweet>();
                }
            }
 
            set
            {
                if (HttpContext.Current.Cache[m_Query] == null)
                {
                    HttpContext.Current.Cache.Add(m_Query, value, null, DateTime.Now.AddMinutes(5),
                        Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                }
                else
                {
                    HttpContext.Current.Cache[m_Query] = value;
                }
            }
        }
 
        public string CachedSinceId
        {
            get
            {
                if (HttpContext.Current.Cache[m_SinceIdPrefix + m_Query] != null)
                {
                    return (string)HttpContext.Current.Cache[m_SinceIdPrefix + m_Query];
                }
                else
                {
                    return null;
                }
            }
 
            set
            {
                if (HttpContext.Current.Cache[m_SinceIdPrefix + m_Query] == null)
                {
                    HttpContext.Current.Cache.Add(m_SinceIdPrefix + m_Query, value, null, DateTime.Now.AddMinutes(5),
                        Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
                }
                else
                {
                    HttpContext.Current.Cache[m_SinceIdPrefix + m_Query] = value;
                }
 
            }
        }
 
        private string m_Query = null;
        private readonly string m_DefaultProfileImage = "http://static.twitter.com/images/default_profile_normal.png";
        private readonly string m_SinceIdPrefix = "SinceId::";
        private readonly string m_UrlTemplate = "http://search.twitter.com/search.atom?q={0}&since_id={1}";
        private static XNamespace m_AtomNS = "http://www.w3.org/2005/Atom";
        private static Regex m_TwitterName = new Regex(@"@([\w_]+)", RegexOptions.Compiled);
        private static Regex m_Url = new Regex(@"(https?://([\w-]+\.)+[\w-]+([^ ]*))", RegexOptions.Compiled);
 
        #endregion
 
        #region CreateChildControls
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
 
            try
            {
                AddStylesheet();
                BuildQuery();
                DisplayResults(GetTweets());
            }
            catch (Exception ex)
            {
                this.Controls.Add(new LiteralControl(ex.Message + "<br />" +
                    ex.StackTrace + "<br />" + ex.Source));
            }
        }
        #endregion
 
        #region AddStylesheet
        /// <summary>
        /// Add stylesheet reference to page's head section
        /// </summary>
        private void AddStylesheet()
        {
            //Dynamically add css reference
            HtmlLink cssLink = new HtmlLink();
            cssLink.Href = "~/_layouts/MattJimison/css/twitter.css";
            cssLink.Attributes.Add("rel", "Stylesheet");
            cssLink.Attributes.Add("type", "text/css");
            cssLink.Attributes.Add("media", "all");
            Page.Header.Controls.Add(cssLink);
        }
        #endregion
 
        #region BuildQuery
        /// <summary>
        /// Build dynamic query or read from querystring
        /// </summary>
        private void BuildQuery()
        {
            //If Querystring variable is set, read from QueryString
            if (!String.IsNullOrEmpty(QueryStringVariable) &&
                !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[QueryStringVariable]))
            {
                m_Query = HttpContext.Current.Request.QueryString[QueryStringVariable];
            }
 
            else
            {
                //Dynamically Build Query
                List<string> patterns = new List<string>();
                if (!String.IsNullOrEmpty(SearchText))
                    patterns.Add(SearchText);
 
                if (!String.IsNullOrEmpty(From))
                    patterns.Add("from:" + From);
 
                if (!String.IsNullOrEmpty(To))
                    patterns.Add("to:" + To);
 
                if (!String.IsNullOrEmpty(About))
                    patterns.Add("@" + About);
 
                if (!String.IsNullOrEmpty(HashTag))
                    patterns.Add("#" + HashTag);
 
                //Join patterns with + delimiter
                m_Query = String.Join("+", patterns.ToArray());
 
                //UrlEncode
                m_Query = HttpUtility.UrlEncode(m_Query);
 
            }
        }
        #endregion
 
        #region GetTweets
        /// <summary>
        /// Obtain list of tweets from Twitter search API and merge with cached
        /// items using the previous SinceId
        /// </summary>
        /// <returns>Returns combined list of tweets</returns>
        private List<Tweet> GetTweets()
        {
            List<Tweet> results = new List<Tweet>();
 
            //Do not search on empty  query
            if (String.IsNullOrEmpty(m_Query))
                return results;
 
            //Retrieve cached results from same query
            List<Tweet> oldResults = CachedTweets;
 
            //Load in the search results
            XDocument xDoc = XDocument.Load(string.Format(m_UrlTemplate, m_Query, CachedSinceId));
 
            //Populate the Tweet list
            //Users that have default image won't have this image in search results so that is what
            //DefaultIfEmtpy(m_DefaultProfileImage).First() is for, to point to static image
            List<Tweet> newResults =
            (from tweet in xDoc.Descendants(m_AtomNS + "entry")
             select new Tweet
             {
                 Title = (string)tweet.Element(m_AtomNS + "title"),
                 Published =
                    DateTime.Parse((string)tweet.Element(m_AtomNS + "published")),
                 Id = (string)tweet.Element(m_AtomNS + "id"),
                 Link = tweet.Elements(m_AtomNS + "link")
                    .Where(link => (string)link.Attribute("rel") == "alternate")
                    .Select(link => (string)link.Attribute("href"))
                    .First(),
                 ProfileImage = tweet.Elements(m_AtomNS + "link")
                    .Where(link => (string)link.Attribute("rel") == "image")
                    .Select(link => (string)link.Attribute("href"))
                    .DefaultIfEmpty(m_DefaultProfileImage).First(),
                 Author = (from author in tweet.Descendants(m_AtomNS + "author")
                           select new Author
                           {
                               Name = (string)author.Element(m_AtomNS + "name"),
                               Uri = (string)author.Element(m_AtomNS + "uri"),
                           }).First()
             }
             ).ToList();
 
            //Concat old and new values and sort by id descending
            results = oldResults.Concat(newResults).OrderByDescending(tweet => tweet.Id).ToList();
 
            //Cache results when present
            if (results.Count > 0)
            {
                //Split the id up to grab the numerical portion
                //Example of how it comes through: tag:search.twitter.com,2005:1282047900
                string[] idParts = results[0].Id.Split(':');
                CachedSinceId = idParts[idParts.Length - 1];
                CachedTweets = results;
            }
 
            return results;
        }
        #endregion
 
        #region DisplayResults
        /// <summary>
        /// Creates dynamic controls containing data
        /// </summary>
        /// <param name="results">List of tweets to display</param>
        private void DisplayResults(List<Tweet> results)
        {
            //Variable setup
            Literal content = new Literal();
            StringBuilder txt = new StringBuilder();
            string profileImage = null;
            string authorName = null;
            string authorUri = null;
            string publishDate = null;
            string link = null;
            string title = null;
            string bodyClass = null;
 
            //Parent Panel
            Panel panel = new Panel();
            panel.CssClass = "tweet-searchheader";
 
            //Display Header
            if (!String.IsNullOrEmpty(m_Query))
            {
                //Decode since m_Query is url-encoded and htmlencode for protection
                txt.Append("<div class='tweet-search'><strong>Search Terms:</strong> " +
                    HttpUtility.HtmlEncode(HttpUtility.UrlDecode(m_Query)) + "</div>");
            }
 
            //Display no results found
            if (results.Count == 0)
            {
                txt.Append("<div class=\"tweet-emptysearch\">The search returned no results.</div>");
            }
 
            //Display header
            content.Text = txt.ToString();
            panel.Controls.Add(content);
            this.Controls.Add(panel);
 
            //Return on empty queries
            if (results.Count == 0)
            {
                return;
            }
 
            //start ol
            txt = new StringBuilder();
            txt.Append("<ol class=\"tweet-statuses\">");
 
            //Iterate through results and add to controls
            foreach (Tweet result in results)
            {
                //Set variables
                authorName = result.Author.Name.Replace("'", "");
                authorUri = result.Author.Uri;
                publishDate = result.Published.ToString("G");
                link = result.Link;
                title = AddHyperlinks(result.Title);
 
                //Start li
                txt.Append("<li class=\"tweet-status\">");
 
                //Image handling only shows when images are to be displayed
                if (DisplayImage)
                {
                    profileImage = result.ProfileImage;
                    txt.AppendFormat("<span class=\"tweet-thumb\"><a class=\"tweet-url\" href=\"{0}\" " +
                        "target=\"_blank\"><img class=\"tweet-photo\" src=\"{1}\" alt=\"{2}\" /></a></span>",
                        authorUri, profileImage, authorName);
                }
 
                //Author Link
                bodyClass = (DisplayImage) ? "tweet-statusbody" : "tweet-statusbodyplain";
                txt.AppendFormat("<span class=\"{0}\"><strong><a class=\"screenname\" href=\"{1}\" target=\"_blank\">{2}</a></strong> ",
                    bodyClass, authorUri, authorName);
 
                //Status Message
                txt.AppendFormat("<span class=\"tweet-entrycontent\">{0}</span> ", title);
 
                //Footer Message
                txt.AppendFormat("<span class=\"tweet-entrymeta\"><a href=\"{0}\" class=\"tweet-entrydate\" target=\"_blank\">Posted at {1}</a></span></span>",
                    link, publishDate);
 
                //Clear Element
                txt.Append("<div class=\"clear\"></div>");
 
                //End li
                txt.Append("</li>");
            }
 
            //end ol
            txt.Append("</ol>");
 
            //Add Content
            panel = new Panel();
            panel.CssClass = "tweet-container";
            content = new Literal();
            content.Text = txt.ToString();
            panel.Controls.Add(content);
            this.Controls.Add(panel);
 
        }
        #endregion
 
        #region AddHyperlinks
        /// <summary>
        /// Replace @username and hyperlink text with html anchor tags
        /// </summary>
        /// <param name="text">status text</param>
        /// <returns>status text with auto hyperlinks</returns>
        private string AddHyperlinks(string text)
        {
            text = m_Url.Replace(text, "<a href=\"$1\" target=\"_blank\">$1</a>");
            text = m_TwitterName.Replace(text, "<a href=\"http://www.twitter.com/$1\" target=\"_blank\">@$1</a>");
            return text;
        }
        #endregion
 
    }
}

Closing Thoughts:

This was a fun project to create. There are already so many great Twitter applications out there, and I thought it would be fun to get a WebPart started. There was a lot more I wanted to do, but I might save that for a future post. Although you could derive a solution using the oob xml webpart, it would not be as configurable, utilize Since_Id that the Twitter API recommends, or be easy for a non-developer to work with. I see this webpart as a great way to keep tabs on what people are saying about something (your company for instance) or focus in on some key individual communications while remaining on your own site. Thanks for reading, and be sure to look me up on Twitter.

Cheers.

Matt

Matt Jimison

Microsoft 365 Geek - Husband, father, lover of basketball, football, smoking / grilling, music, movies, video games, and craft beer!

Leave a Reply

Your email address will not be published. Required fields are marked *