Download now: WSP Solution | Source Code (zip)
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: (Click for larger images)
Here are the inspirations for my post, including some of the source code for querying the Twitter Search API via Linq:
- Jan Tielens’ The Twitter Search API made easy with Linq to XML
- Robert Horvick’s Reading Twitter Data with C# and LINQ
Here is how I made this project my own:
- Created a solution that contains a site-scoped feature for adding a Twitter Search WebPart to your site (WSP available for Download)
- WebPart has the main Twitter search API query abilities:
- General search term
- From user
- To user
- About user
- Hashtag
- Added the ability to define a QueryString variable that overrides the webpart configuration and takes the search directly from the QueryString
- Used caching in combination with Since_Id to cut back on amount of data being brought back with each request
- Solution deploys a custom css file to the 12 hive, which is embedded into the head of pages that include the webpart (easy to customize for your style)
- Enhanced the Linq query to bring back the profile images (or substitute Twitter’s default image when one is not present)
- Added the ability to turn profile images on and off in results display
- Used regular expressions to hyperlink usernames and urls embedded in status updates (text search returns has html stripped)
- 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
The source code and wsp file are both available for download. You will need WSPBuilder installed if you wish to work with the provided source code, as that is my choice of development tool, and since Linq, lambda expressions, etc… are used, you will also need the ASP.Net 3.5 Framework to run this webpart.
Installation Instructions:
- Download wsp to your server
- Run the following stsadm commands on the solution file (Best to run in directory where you have copied the solution)
- stsadm -o addsolution -filename MattJimison.Twitter.wsp
- stsadm -o deploysolution -name MattJimison.Twitter.wsp -url http://yoursite.com -immediate -allowgacdeployment
- stsadm -o execadmsvcjobs
- stsadm -o activatefeature -filename TwitterSearchWebPart\feature.xml -url http://yoursite.com
- The WebPart will be located under the group ‘mattjimison.com’
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
Downloads
WSP Solution
Source Code (zip)
Source code for main webpart class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | 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 } } |